Game Development Reference
In-Depth Information
numPoints:(int)numPoints
{
CGFloat deltaAngle = 360.0 / numPoints;
CGFloat angle = 360.0;
for (int i = 1; i <= numPoints; i++)
{
CGPoint pos = CGPointMake(
origin.x + radius
* cos(CC_DEGREES_TO_RADIANS(angle)),
origin.y + radius
* sin(CC_DEGREES_TO_RADIANS(angle)));
NSLog(@"circumference point %i: {%.1f, %.1f}
(angle: %.1f)”,
i, pos.x, pos.y, angle);
angle -= deltaAngle;
}
}
The
deltaAngle
in this example will be 45, indicating that the points need to be dis-
tributed on 45-degree angles. The
angle
property is set to
360
and will be counted
down to
0
. This will print the points in clockwise order. If you want counter-clockwise or-
dering, you simply count up the angle variable from 0.
The
for
loop enumerates each point. The loop counter “
i
” is used only to print out the
point's number in the log. The actual calculation for determining a point on a circle's cir-
cumference is written in pseudo-code as shown in
Listing 10-2
.
Listing 10-2
.
Algorithm to determine a point on a circle with a given radius and angle in
radians
x = radius * cos(angle);
y = radius * sin(angle);
This calculation requires that angle is expressed in radians, not degrees. Hence, you per-
form the conversion using the Cocos2D built-in macro
CC_DEGREES_TO_RADIANS(angle)
, which takes an angle in degrees and returns the
angle in radians. Of course, a companion macro to do the same in reverse also exists:
CC_RADIANS_TO_DEGREES(angle)
.
Search WWH ::
Custom Search