Game Development Reference
In-Depth Information
For polar coordinates, the “preferred” way to describe any given point is
known as the canonical coordinates for that point. A 2D polar coordinate
pair (r,θ) is in the canonical set if r is nonnegative and θ is in the interval
(−180 o ,180 o ]. Notice that the interval is half open: for points directly
“west” of the origin (x < 0,y = 0), we will use θ = +180 o . Also, if r = 0
(which is only true at the origin), then we usually assign θ = 0. If you
apply all these rules, then for any given point in 2D space, there is exactly
one way to represent that point using canonical polar coordinates. We can
summmarize this succintly with some math notation. A polar coordinate
pair (r,θ) is in the canonical set if all of the following are true:
r ≥ 0
We don't measure distances “backwards.”
Conditions satisfied by
canonical coordinates
The angle is limited to 1/2 revolution.
We use +180 o for “west.”
−180 o < θ ≤ 180 o
r = 0
θ = 0
At the origin, set the angle to zero.
The following algorithm can be used to convert a polar coordinate pair
into its canonical form:
1. If r = 0, then assign θ = 0.
Converting a polar
coordinate pair (r, θ) to
canonical form
2. If r < 0, then negate r, and add 180 o to θ.
3. If θ ≤ −180 o , then add 360 o to θ until θ > −180 o .
4. If θ > 180 o , then subtract 360 o from θ until θ ≤ 180 o .
Listing 7.1 shows how it could be done in C. As discussed in Sec-
tion 7.1.1, our computer code will normally store angles using radians.
/ /
R a d i a l
d i s t a n c e
f l o a t
r ;
/ /
Angle
i n
RADIANS
f l o a t
t h e t a ;
/ /
D e c l a r e
a
c o n s t a n t
f o r
2 p i
(360
d e g r e e s )
c o n s t
f l o a t
TWOPI
=
2 . 0 f P I ;
/ /
Check
i f
we
a r e
e x a c t l y
a t
t h e
o r i g i n
i f
( r
==
0 . 0 f )
{
/ /
At
t h e
o r i g i n
slam
t h e t a
t o
z e r o
t h e t a
=
0 . 0 f ;
}
e l s e
{
/ /
Handle
n e g a t i v e
d i s t a n c e
i f
( r < 0 . 0 f )
{
Search WWH ::




Custom Search