20

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?

5 Answers 5

30

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)
3
  • 1
    In Lua you can use math.deg and math.rad to convert between degrees and radians.
    – lhf
    Jul 9, 2012 at 13:03
  • @lhf: Thanks. I've never done anything in lua, so I thought this approach would be safe.
    – andand
    Jul 9, 2012 at 14:09
  • import math, followed by: theta = math.radians(angle)
    – tinyMind
    Feb 17, 2020 at 19:14
5

Apparently, something like this will help:

x = cos(theta)
y = sin(theta) 

Simple Google search threw this up, and the guy who asked the question said it solved it.

2
  • Yeah I saw that too. Do you know what the x and y values (calculated by sin and cos) indicate with respect to my question (I have updated the question) ?
    – SatheeshJM
    Jul 9, 2012 at 12:19
  • Make sure theta is in radians, The code given by the OP converts to degrees.
    – lhf
    Jul 9, 2012 at 12:20
2

You'll probably get the wrong numbers if you use:

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

If you are using the coordinate system where y gets bigger going down the screen and x gets bigger going to the right then you should use:

local dy = y1 - y2
local dx = x2 - x1
local angle = math.deg(math.atan2(dy, dx))
if (angle < 0) then
  angle = 360 + angle
end

The reason you want to use this is because atan2 in lua will give you a number between -180 and 180. It will be correct until you hit 180 then as it should go beyond 180 (i.e. 187) it will invert it to a negative number going down from -180 to 0 as you get closer to 360. To correct for this we check to see if the angle is less than 0 and if it is we add 360 to give us the correct angle.

0

According this reference:

Returns the arc tangent of y/x (in radians), but uses the signs of both parameters to find the quadrant of the result. (It also handles correctly the case of x being zero.)

So I guess you can use math.tan to invert it also.

0

As atan2 works as tan-1, so the inverse could be tan, taking into consideration conversion between radian and degree

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.