# Convert from cartesian to polar

This topic is 5466 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a coordinate (x, y) and I want to convert that into an angle and length. How's that done? :)

##### Share on other sites
I guess I should have looked elsewhere first, but for the benefit of others, here's what I found:

Polar from Cartesian:

R = Sqrt(x2 + y2);
Theta = ArcTan(Y / X);

Cartesian From Polar:

X = R * cos(Theta)
Y = R * sin(Theta)

Any comments on optimizing for speed? Ie, can that atan() be changed?

[edited by grhodes_at_work to put the "2"'s in superscripts for the R equation]

[Edited by - grhodes_at_work on October 1, 2004 4:23:27 PM]

##### Share on other sites
If you're using c/c++, Make sure you use atan2() and not atan().
atan() returns an angle in the interval 0 <= theta <= pi/2. You probably want an angle in the interval 0 <= theta <= 2*pi. Use atan2() to get that. If you're using a programming language like Visual Basic yiu'll have to transform the angle yourself based on the signs of x and y.

If your X and Y are always greater than 0 you can ignore this post ;)

##### Share on other sites
Pre-calculated look up table would probably give you enough accuracy

##### Share on other sites
Hmm, the Visual C++ help says that atan2 returns the arctangent of y/x in the range –π to π radians.

##### Share on other sites
Quote:
 Original post by griminventionsHmm, the Visual C++ help says that atan2 returns the arctangent of y/x in the range –ð to ð radians.

Yep. That's your correct angle. If you want a range 0 to PI, then simply add 2PI when the result of atan2 is negative and leave it alone when zero or positive. You already have the equation for R in your second post!

##### Share on other sites
Cool, I've got it now. Thanks for being patient with me, guys. :)

##### Share on other sites
It's only basic trigonometry and Pythagoras' theorem

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 22
• 13
• 14
• 45