Misc: Use the ^ operator instead of math.pow (#9550)

And some minor code simplifications, thanks to @zwim ;).
pull/9566/head
NiLuJe 2 years ago committed by GitHub
parent 62059f8d68
commit 4d48b6e2fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -890,8 +890,8 @@ function ReaderLink:onGoToPageLink(ges, internal_links_only, max_distance)
local shortest_dist = nil
for _, link in ipairs(links) do
if not internal_links_only or link.page then
local start_dist = math.pow(link.x0 - pos_x, 2) + math.pow(link.y0 - pos_y, 2)
local end_dist = math.pow(link.x1 - pos_x, 2) + math.pow(link.y1 - pos_y, 2)
local start_dist = (link.x0 - pos_x)^2 + (link.y0 - pos_y)^2
local end_dist = (link.x1 - pos_x)^2 + (link.y1 - pos_y)^2
local min_dist = math.min(start_dist, end_dist)
if shortest_dist == nil or min_dist < shortest_dist then
-- onGotoLink()'s GotoPage event needs the link
@ -980,17 +980,17 @@ function ReaderLink:onGoToPageLink(ges, internal_links_only, max_distance)
-- and we compute each part individually
-- First, vertical distance (squared)
if pos_y < segment.y0 then -- above the segment height
segment_dist = math.pow(segment.y0 - pos_y, 2)
segment_dist = (segment.y0 - pos_y)^2
elseif pos_y > segment.y1 then -- below the segment height
segment_dist = math.pow(pos_y - segment.y1, 2)
segment_dist = (pos_y - segment.y1)^2
else -- gesture pos is on the segment height, no vertical distance
segment_dist = 0
end
-- Next, horizontal distance (squared)
if pos_x < segment.x0 then -- on the left of segment: calc dist to x0
segment_dist = segment_dist + math.pow(segment.x0 - pos_x, 2)
segment_dist = segment_dist + (segment.x0 - pos_x)^2
elseif pos_x > segment.x1 then -- on the right of segment : calc dist to x1
segment_dist = segment_dist + math.pow(pos_x - segment.x1, 2)
segment_dist = segment_dist + (pos_x - segment.x1)^2
-- else -- gesture pos is in the segment width, no horizontal distance
end
if shortest_dist == nil or segment_dist < shortest_dist then
@ -1013,8 +1013,8 @@ function ReaderLink:onGoToPageLink(ges, internal_links_only, max_distance)
-- We used to just check distance from start_x and end_x, and
-- we could miss a tap in the middle of a long link.
-- (also start_y = end_y = the top of the rect for a link on a single line)
local start_dist = math.pow(link.start_x - pos_x, 2) + math.pow(link.start_y - pos_y, 2)
local end_dist = math.pow(link.end_x - pos_x, 2) + math.pow(link.end_y - pos_y, 2)
local start_dist = (link.start_x - pos_x)^2 + (link.start_y - pos_y)^2
local end_dist = (link.end_x - pos_x)^2 + (link.end_y - pos_y)^2
local min_dist = math.min(start_dist, end_dist)
if shortest_dist == nil or min_dist < shortest_dist then
selected_link = link
@ -1050,7 +1050,7 @@ function ReaderLink:onGoToPageLink(ges, internal_links_only, max_distance)
end
end
if selected_link then
if max_distance and selected_distance2 and selected_distance2 > math.pow(max_distance, 2) then
if max_distance and selected_distance2 and selected_distance2 > max_distance^2 then
logger.dbg("nearest link is further than max distance, ignoring it")
else
return self:onGotoLink(selected_link, false, isFootnoteLinkInPopupEnabled())

@ -384,7 +384,7 @@ function ReaderScrolling:_setupAction()
end
-- Decrease velocity at each step
self._velocity = self._velocity * math.pow(self.scroll_friction, self._inertial_scroll_interval)
self._velocity = self._velocity * self.scroll_friction^self._inertial_scroll_interval
local dist = math.floor(self._velocity * self._inertial_scroll_interval)
if math.abs(dist) < self.end_scroll_dist then
-- Decrease it even more so scrolling stops sooner

@ -91,14 +91,12 @@ function SysfsLight:setNaturalBrightness(brightness, warmth)
if brightness > 0 then
-- On Nickel, the values for white/red/green are roughly linearly dependent
-- on the 4th root of brightness and warmth.
white = math.min(self.white_gain * math.pow(brightness, self.exponent) *
math.pow(100 - warmth, self.exponent) + self.white_offset, 255)
white = math.min(self.white_gain * (brightness * (100 - warmth))^self.exponent + self.white_offset, 255)
end
if warmth > 0 then
red = math.min(self.red_gain * math.pow(brightness, self.exponent) *
math.pow(warmth, self.exponent) + self.red_offset, 255)
green = math.min(self.green_gain * math.pow(brightness, self.exponent) *
math.pow(warmth, self.exponent) + self.green_offset, 255)
local brightness_warmth_exp = (brightness * warmth)^self.exponent
red = math.min(self.red_gain * brightness_warmth_exp + self.red_offset, 255)
green = math.min(self.green_gain * brightness_warmth_exp + self.green_offset, 255)
end
white = math.max(white, 0)

@ -33,7 +33,7 @@ local logger = require("logger")
local function get_dpi_scale()
local size_scale = math.min(Screen:getWidth(), Screen:getHeight()) / 600
local dpi_scale = Screen:scaleByDPI(1)
return math.pow(2, math.max(0, math.log((size_scale+dpi_scale)/2)/0.69))
return math.max(0, (math.log((size_scale+dpi_scale)/2)/0.69)^2)
end
local DPI_SCALE = get_dpi_scale()

Loading…
Cancel
Save