Converting cartesian to polar coordinates

1.5k Views Asked by At

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?

1

There are 1 best solutions below

3
On

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$