Difference between revisions of "Rotating A Point In 2D In Lua"

From GPWiki
Jump to: navigation, search
m (Reducing the width of the code.)
m (+cat + stub)
 
Line 1: Line 1:
 +
{{stub}}
 +
 
<syntaxhighlight type="lua">
 
<syntaxhighlight type="lua">
 
-- math.cos and math.sin expect a radian value, and this little function converts degrees to radians
 
-- math.cos and math.sin expect a radian value, and this little function converts degrees to radians
Line 10: Line 12:
 
end
 
end
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
[[Category:Lua]]

Latest revision as of 12:57, 10 February 2013

40px-sprout.png   This article is a stub. You can help out by expanding it.


-- math.cos and math.sin expect a radian value, and this little function converts degrees to radians
toRadians = function(degrees) return degrees / 180 * math.pi end 
 
function rotatePoint(point, origin, degrees)
    local x = origin.x + ( math.cos(toRadians(degrees)) * (point.x - origin.x) - math.sin(toRadians(degrees)) * (point.y - origin.y) )
    local y = origin.y + ( math.sin(toRadians(degrees)) * (point.x - origin.x) + math.cos(toRadians(degrees)) * (point.y - origin.y) )
    point.x = x
    point.y = y
end