Convert from cartesian to polar

Started by
6 comments, last by furby100 19 years, 6 months ago
I have a coordinate (x, y) and I want to convert that into an angle and length. How's that done? :)
Advertisement
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]
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 ;)
Pre-calculated look up table would probably give you enough accuracy
Hmm, the Visual C++ help says that atan2 returns the arctangent of y/x in the range –π to π radians.
Quote:Original post by griminventions
Hmm, 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!
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Cool, I've got it now. Thanks for being patient with me, guys. :)
It's only basic trigonometry and Pythagoras' theorem

This topic is closed to new replies.

Advertisement