Game Development Reference
In-Depth Information
Figure 7.4
Converting between Cartesian and polar
coordinates
Notice that aliasing is a nonissue; Equation (7.1) works even for “weird”
values of r and θ.
Computing the polar coordinates (r,θ) from the Cartesian coordinates
(x,y) is the tricky part. Due to aliasing, there isn't only one right answer;
there are infinitely many (r,θ) pairs that describe the point (x,y). Usually,
we want the canonical coordinates.
We can easily compute r by using the Pythagorean theorem,
r =
x 2 + y 2 .
Since the square root function always returns the positive root, we don't
have to worry about r causing our computed polar coordinates to be outside
the canonical set.
Computing r was pretty easy, so now let's solve for θ:
y
x = r sinθ
r cosθ ,
y
x = sinθ
cosθ ,
y/x = tanθ,
θ = arctan(y/x).
Unfortunately, there are two problems with this approach. The first is
that if x = 0, the division is undefined. The second is that the arctan
function has a range of only [−90 o ,+90 o ]. The basic problem is that the
division y/x effectively discards some useful information. Both x and y
can either be positive or negative, resulting in four different possibilities,
corresponding to the four different quadrants that may contain the point.
But the division y/x results in a single value. If we negate both x and y,
 
Search WWH ::




Custom Search