Skip to content

Instantly share code, notes, and snippets.

@abaez
Last active December 29, 2015 05:39
Show Gist options
  • Save abaez/7623220 to your computer and use it in GitHub Desktop.
Save abaez/7623220 to your computer and use it in GitHub Desktop.
Find the ppi, area, and dimensions of a screen.
#!/usr/bin/env lua
--- returns the ppi from the resolution and diagonical inches of the screen.
-- @func ppi
-- @param inch is the diagonal inches of screen.
-- @param width is the width of native resolution.
-- @param height is the height of native resolution.
function ppi(inch, width, height)
return math.sqrt(width^2 + height^2) / inch -- ppi = pixels/inch
end
--- returns the physical width and height of screen in inches.
-- @func ss
-- @param inch is the diagonal inches of screen.
-- @param width is the width of native resolution.
-- @param height is the height of native resolution.
function ss(inch, width, height)
local ip = inch / math.sqrt(width^2 + height^2) -- ip = inches/pixels
-- returns width and height in inches.
return ip * width, ip * height
end
--- gets the area of the screen.
-- @func area
-- @param inch is the diagonal inches of screen.
-- @param width is the width of native resolution.
-- @param height is the height of native resolution.
function area(inch, width, height)
local total = 1
--[[ note: using ipairs{} to declare a constructed table from the
return of the closure ss. This way save time and memory in run. ]]
for _,v in ipairs{ss(inch, width, height)} do
total = total * v
end
return total
end
--- initializer for parameter manipulation through closure constructor.
-- Uses the parameters by closure initialization.
-- @func area
-- @param inch is the diagonal inches of screen.
-- @param width is the width of native resolution.
-- @param height is the height of native resolution.
-- @param fn function that manipulates the parameters after closure declare.
dimensions = function(inch, width, height)
width = width or 1920
height = height or 1080
return function(fn)
return fn(inch, width, height)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment