Converting cartesian to polar coordinates

$\begingroup$

I am trying to find a way to convert cartesian to polar coordinates.

A quick search gives me:

$$r^2=x^2+y^2$$

$$θ=\arctan(y/x)$$

so far so good, but what happens when $x=0$. I know in that case theta is $\pi/2$ but how can I compute that from a computer point of view? or should I just make a special case?

$\endgroup$ 5

1 Answer

$\begingroup$

read $ x$

read $ y$

$r :=\sqrt{x^2+y^2}$

if $ x> 0 $ then $\theta=\arctan(\frac yx)$

if $ x<0 $ then $ \theta=\arctan(\frac yx) +\pi$

if $ x=0 $ and $y>0$ then $\theta=\frac \pi 2$

if $ x=0$ and $y<0$ then $\theta = -\frac \pi 2$

$\endgroup$ 3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like