Search code examples
mathluatrigonometry

Inverse of math.atan2?


What is the inverse of the function

math.atan2

I use this in Lua where I can get the inverse of math.atan by math.tan.
But I am lost here.

EDIT

OK, let me give you more details.

I needed to calculate angle between 2 points (x1,y1) and (x2,y2) and I did,

local dy = y1-y2 
local dx = x1-x2
local angle = atan2(dy,dx)* 180 / pi

Now if I have the angle, is it possible to get back dy and dx?


Solution

  • Given only the angle you can only derive a unit vector pointing to (dx, dy). To get the original (dx, dy) you also need to know the length of the vector (dx, dy), which I'll call len. You also have to convert the angle you derived from degrees back to radians and then use the trig equations mentioned elsewhere in this post. That is you have:

    local dy = y1-y2
    local dx = x1-x2
    local angle = atan2(dy,dx) * 180 / pi
    local len = sqrt(dx*dx + dy*dy)
    

    Given angle (in degrees) and the vector length, len, you can derive dx and dy by:

    local theta = angle * pi / 180
    local dx = len * cos(theta)
    local dy = len * sin(theta)