Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 238
SatheeshJM Avatar asked Jul 09 '12 12:07

SatheeshJM


People also ask

What does math atan2 return?

The Math. atan2() function returns the angle in the plane (in radians) between the positive x-axis and the ray from (0, 0) to the point (x, y), for Math. atan2(y, x) .

Is atan2 the same as Arctan?

Threadsafe: Yes. The atan() and atan2() functions calculate the arctangent of x and y/x , respectively. The atan() function returns a value in the range -π/2 to π/2 radians. The atan2() function returns a value in the range -π to π radians.

What is atan2 formula?

The function atan2 computes the principal value of the argument function applied to the complex number x + i y. That is, atan2(y, x) = Pr arg(x + i y) = Arg(x + i y).

What is atan2 on a calculator?

ATAN2(y,x) returns the arc tangent of the two numbers x and y. It is similar to calculating the arc tangent of y / x, except that the signs of both arguments are used to determine the quadrant of the result. The result is an angle expressed in radians. To convert from radians to degrees, use the DEGREES function.


3 Answers

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)
like image 156
andand Avatar answered Oct 18 '22 19:10

andand


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.

like image 33
Nathan White Avatar answered Oct 18 '22 18:10

Nathan White


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.

like image 2
Lokiare Avatar answered Oct 18 '22 18:10

Lokiare