Created
August 10, 2016 05:06
-
-
Save kirubz/fa84375008d376a2d695618e0ae3aed8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- returns the degrees between (0,0) and pt (note: 0 degrees is 'east') | |
function angleOfPoint( pt ) | |
local x, y = pt.x, pt.y | |
local radian = math.atan2(y,x) | |
local angle = radian*180/math.pi | |
if angle < 0 then angle = 360 + angle end | |
return angle | |
end | |
-- returns the degrees between two points (note: 0 degrees is 'east') | |
function angleBetweenPoints( a, b ) | |
local x, y = b.x - a.x, b.y - a.y | |
return angleOfPoint( { x=x, y=y } ) | |
end | |
-- returns the smallest angle between the two angles | |
-- ie: the difference between the two angles via the shortest distance | |
function smallestAngleDiff( target, source ) | |
local a = target - source | |
if (a > 180) then | |
a = a - 360 | |
elseif (a < -180) then | |
a = a + 360 | |
end | |
return a | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment