| **Convert cartesian coordinates to polar**
on Nov 25, 2014 at 5:34:32 pm |

What's the most efficent way to convert a position value:

` [x value, y value] `

into a polar vector?

` [magnitude, angle] `

(I want to perform an operation such as multiplying the angle and/or magnitude, and then convert back to cartesian)

| **Re: Convert cartesian coordinates to polar** on Nov 25, 2014 at 6:02:14 pm |

If you want it relative to the comp's [0,0] (upper left corner), I think it would be something like

[length(xyArray),radiansToDegrees(Math.atan2(xyArray[1],xyArray[0]))]

Dan

| **Re: Convert cartesian coordinates to polar** on Nov 25, 2014 at 8:42:19 pm |

Dan's answer is entirely correct, but maybe this can help you understand some of the math involved.

--

Using simple trigonometry:

Imagine you have the point [960, 540];

Those 2 values, 960 and 540, represent two adjacent sides of a triangle.

Now, if you recall back to 9th Grade, you may remember that there is a simple equation to get the third side of that triangle. A² + B² = C². So, we can find the third side (the magnitude) by using an equation like:

var mag = Math.sqrt(Math.exp(position[0], 2) + Math.exp(position[1], 2));

Essentially saying take the square root of X² + Y².

Fortunately, AE gives us the handy little 'length' operator, which simply does all of these computations for us.

--

Another similar equation can be used to find the angle of a triangle when you know two other sides, you may remember SOH CAH TOA.

Imagine the angle at the top left of your comp, now notice that you have the opposite side (the Y coord) and the adjacent side (the X coord). Since TOA uses both opposite and adjacent sides, we can get that angle using an equation like this:

tan(theta) = opposite/adjacent

tan(theta) = 540/960

theta = tan−1(5625)

or, in code

var angle = Math.atan(position[1] / position[0]);

since this will return a value in radians instead of degrees, we need to convert it.

var angle_deg = radiansToDegrees(angle);

--

As mentioned above, the code Dan posted in the previous post will do you just fine. I just find it helpful to visualize the math when approaching coding problems – especially those involving geometry/trig.

Good luck!

David Conklin

Motion Designer