Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Convert cartesian coordinates to polar

  • Convert cartesian coordinates to polar

    Posted by Marcus Round on November 25, 2014 at 5:34 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)

    Marcus Round replied 10 years, 4 months ago 3 Members · 3 Replies
  • 3 Replies
  • Dan Ebberts

    November 25, 2014 at 6:02 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

  • David Conklin

    November 25, 2014 at 8:42 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

  • Marcus Round

    November 26, 2014 at 9:26 am

    Thank you both.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy