Scripting Helpers is winding down operations and is now read-only. More info→

Log in to vote

3
May someone explain math.atan2 and it's capabilities. Please provide an example if you can.

Log in to vote

15
I will assume working knowledge of...

- tangent (hereby referred to as
`tan`

), - cosine (hereby referred to as
`cos`

), - sine (hereby referred to as
`sin`

), - radians,
- and the four-quadrant 2D Cartesian coordinate system.

You can read about basic trigonometric functions, radians, and the four-quadrant 2D Cartesian coordinate system with the links provided in this sentence.

In trigonometry, we can tell the sign of `trigonometryFunction(x)`

on some number x simply by looking at which quadrant `x`

is in.

`sin`

is positive for quadrants I and II, and negative for quadrants III and IV. In other words, the quadrants above the x-axis are where`sin`

is positive.`cos`

is positive for quadrants I and IV, and negative for quadrants II and III. In other words, the quadrants to the right of the y-axis are where`cos`

is positive.

From trigonometry, we define tan to be the following function: `tan(x) = sin(x) / cos(x)`

.

This brings about a rather interesting relationship, because this means that the sign of `tan`

is dependent on the sign of `sin`

and `cos`

, because `tan`

is *defined in terms of * `sin`

and `cos`

. We can derive the sign of `tan(x)`

by first deriving the sign of `sin(x)`

and `cos(x)`

, and then dividing the sign of `sin(x)`

by the sign of `cos(x)`

.

With this relationship in mind, we get the following:

`tan`

is positive for quadrants I and III, and negative for quadrants II and IV. In other words, the quadrants where the line y = -x resides within are where`tan`

is positive.

These sign quadrants are not particularly important for many things, but they are incredibly important when calculating the values of the inverse trigonometric functions.

As the name suggests, the basic inverse trigonometric functions are used to find the angle fed into a trigonometric function that produces a number bounded between -1 and 1. They are typically called arc sine (hereby referred to as `asin`

), arc cosine (hereby referred to as `acos`

), and arc tangent (hereby referred to as `atan`

).

This brings a common misunderstanding, because then people start thinking that the following relationships are true...

`asin(sin(x)) = x`

, given an angle`x`

bounded from`0`

to`2pi`

.`acos(cos(x)) = x`

, given an angle`x`

bounded from`0`

to`2pi`

.`atan(tan(x)) = x`

, given an angle`x`

bounded from`0`

to`2pi`

.

Unfortunately, these relationships do *not* hold for all `x`

. The reasoning lies behind the words inverse trigonometric functions and the quadrant signs that we derived above.

First, notice that the signs for the quadrants must be either negative or positive for all of the basic trigonometric functions.

Second, notice that there are exactly two positive quadrants and two negative quadrants for all of the basic trigonometric functions.

Third (and this one takes a bit of thinking), notice that the range of the absolute value of the trigonometric functions repeat multiple times within the domain `0`

to `2pi`

(exactly twice for this domain, to verify plot the graphs of `tan(x)`

, `sin(x)`

, and `cos(x)`

). This is best illustrated with multiples of k such that `pi/4 + k*pi/2`

that lie in the domain (so `pi/4`

, `3pi/4`

, `5pi/4`

, `7pi/4`

).

Notice that if you take those four numbers and use the basic trigonometric functions on them, you get the following result...

`sin`

produces`1/sqrt(2)`

,`1/sqrt(2)`

,`-1/sqrt(2)`

, and`-1/sqrt(2)`

, respectively.`cos`

produces`1/sqrt(2)`

,`-1/sqrt(2)`

,`-1/sqrt(2)`

, and`1/sqrt(2)`

, respectively.`tan`

produces`1`

,`-1`

,`1`

, and`-1`

, respectively.

Notice that the results of each trigonometric function produces only *two* different numbers as outputs, even though we gave *four* different numbers as inputs.

If you know anything about functions in mathematics, you know that each input into a function must produce only a single output. *The general idea is that the number of unique inputs must be greater than or equal to the number of unique outputs*.

So, the results above are okay for trigonometric functions because we gave it four numbers, and got two numbers. Four is less than two. Good.

However, if we switch the results around like so to get the possible results of the basic inverse trigonometric functions...

`asin`

given`1/sqrt(2)`

,`-1/sqrt(2)`

produces (`pi/4`

,`3pi/4`

) and (`5pi/4`

,`7pi/4`

) respectively.`acos`

given`1/sqrt(2)`

,`-1/sqrt(2)`

produces (`pi/4`

,`7pi/4`

) and (`3pi/4`

,`5pi/4`

) respectively.`atan`

given`1`

,`-1`

produces (`pi/4`

,`5pi/4`

) and (`3pi/4`

,`7pi/4`

) respectively.

Notice how for each input, we get two outputs. This *violates* the definition of a function.

However, we still want to be able to get the equivalent original angle, and inverse trigonometric functions are our best bet. How can we turn our inverse trigonometric not-really-functions into functions?

The answer lies in restricting the range. Notice that our range was originally bound from `0`

to `2pi`

. What if we chose our range carefully such that we throw away half of our outputs and end up with only one output per input?

- For
`asin`

, we will pick the range`-pi/2`

to`pi/2`

. - For
`acos`

, we will pick the range`0`

to`pi`

. - For
`atan`

, we will pick the range`-pi/2`

to`pi/2`

.

Notice that if you overlay each inverse trigonometric function's range with the domain of its corresponding trigonometric function, the range *only* covers a single positive quadrant and a single negative quadrant. This is how we effectively throw out half of our outputs; it turns out that the reason we were getting two outputs per input was because we were including the answers from two quadrants at a time!

When we throw out half of our outputs from before, we lose half of the information. This means that we aren't quite sure *what* quadrant the angle was in; `atan`

could have given us the right angle or it could have given us an angle `pi`

radians off. Ouch.

Let me give you an example that I think is the best example of a perfect opportunity to use `atan2`

over `atan`

.

Let `<x, y>`

be a two-dimensional vector on a Cartesian plane where the vector's value along the domain is x and the vector's value along the range is y.

Can you find the angle `<-1, sqrt(3)>`

makes with the x-axis? This relies on the geometric definition of tangent...

- the tangent of an acute angle in a right triangle is equivalent to the length of the opposite leg divided by the length of the adjacent leg.

We compute `atan(-sqrt(3)/1)`

and we get `-pi/3`

... which is the wrong answer. Why? `-pi/3`

lies in the fourth quadrant, while `<-1, sqrt(3)>`

is in the second quadrant.

Huh. That's strange. How about, `<1, -sqrt(3)>`

? We computer `atan(sqrt(3)/-1)`

and we get `-pi/3`

as well... and that's the right answer.

Notice that `atan(sqrt(3)/-1)`

is equivalent to `atan(-sqrt(3)/1)`

because `sqrt(3)/-1`

is equal to `-sqrt(3)/1`

, which is also equal to `-sqrt(3)`

.

This brings us to the conclusion that *if the input of atan involves negative numbers in its numerator or denominator, then atan may give us a result off by pi*.

This is because the range of `atan`

*only* covers the first and fourth quadrants. If our input is in the second or third quadrant, our `atan`

will be off.

In case you didn't notice, *this is bad*. *Really bad* (see why in the section below).

We need something more exact; something that doesn't require us to do more calculations to verify our output is right or not.

In `sin`

or `cos`

, this isn't as much of a problem because they only take into account either the x or the y axis according to the geometric definition of `sin`

and `cos`

:

- the sine of an acute angle in a right triangle is equivalent to the length of the opposite leg divided by the length of the hypotenuse.
- the cosine of an acute angle in a right triangle is equivalent to the length of the adjacent leg divided by the length of the hypotenuse.

Notice that both involve dividing by the length of the hypotenuse. This will always be positive, because of the Pythagorean theorem, where `a^2 + b^2 = c^2`

.

Since the hypotenuse is always positive, then a negative input will indicate that...

- in the case of
`sin`

, the x-axis is negative - in the case of
`cos`

, the y-axis is negative

Therefore, we can **always** tell if the input is off by `pi`

for `sin`

and `cos`

.

In the case of tangent, we cannot get that same detailed information because both the numerator and the denominator of the input may be negative. Therefore, we are at a loss and `atan`

just cannot give us the exact angle the input represents.

`atan2`

is a function that takes two inputs, so it looks like `atan(y, x)`

.

What makes `atan2`

incredibly easy to remember is that it behaves *exactly* like the problem above; given a vector `<x, y>`

, get the angle it makes with the x-axis.

This means that if we do `atan2(sqrt(3), -1)`

, we get `2pi/3`

. This is perfect! `2pi/3`

is in the second quadrant, and so is `<-1, sqrt(3)>`

.

Because of its definition, `atan2`

has a few quirks. I've listed all that comes to mind below.

- In the case that x is zero,
`atan2`

still works and will correctly return the angle the vector makes with the positive x-axis (as opposed to`atan`

, which will throw an error because you attempted to divide by`0`

). - In the case that both x and y are zero,
`atan`

will return 0 (non-intuitive quirk of atan2).

## Locked by Avigant and DeveloperSolo

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?