I know that you can keep adding/subtracting numbers to a polar coord, but what if I want to be able to take a number and just convert it to its positive equivalent?
Converting a polar coord to the range $0\le\theta\le2\pi?$
3 Answers
I will assume you want $0 \leq \theta < 2\pi$, since $2\pi$ and $0$ are the same angle. This is the modulo operation.
You can write it as $x \operatorname{mod} 2\pi$, or express it via floor function $x-2 \pi \lfloor \frac{x}{2\pi} \rfloor$, which works for both positive and negative $x$. Warning: Some programming languages with floating point modulo operators will give you negative result for negative angle. You can fix that by a single check if (result < 0) then result += 2*pi
.
Theorem: For any real $a,b$ such that $b>0$ there exists exactly one integer $q$ and real $r \in [0,b)$ such that $a=qb+r$.
If your system has an Atan2 function, it will be done for you.
-
0@JohnSmith: I was assuming you had $(x,y)$ coordinates and wanted the angle. If you already have an angle, some systems allow the mod function on real arguments: theta%(2pi) will give something in the range [0,2pi). Otherwise you can use integer division: c=int(theta/(2pi)); theta=theta-c*2pi – 2012-01-26
If I'm right you want a function to get an input of any real number and convert it to it's equivalent angle between $0<\theta< 2\pi$? here is pseudo code how I do it: $ \begin{eqnarray}&\text{while}(\theta<0\text{ or }\theta > 2\pi\text{)} \\ &\text{if(}\theta > 0)\\ &&\theta := \theta - 2\pi\\ &else\\ &&\theta := \theta + 2\pi\\ \end{eqnarray}$