Lab 5 - Views
Objectives
You will be creating a custom view for the polygon lab which will draw the polygons shapes with a subview to display the name of the polygon. We will also save the state of the application so that it is where you left it when you launch it again.
+ (NSArray
*)pointsForPolygonInRect:(CGRect)rect numberOfSides:(int)numberOfSides {
CGPoint center =
CGPointMake(rect.size.width / 2.0, rect.size.height / 2.0);
float radius = 0.9 * center.x;
NSMutableArray *result = [NSMutableArray
array];
float angle = (2.0 * M_PI) /
numberOfSides;
float exteriorAngle = M_PI - angle;
float rotationDelta = angle - (0.5 *
exteriorAngle);
for (int currentAngle = 0;
currentAngle < numberOfSides; currentAngle++) {
float
newAngle = (angle * currentAngle) - rotationDelta;
float curX =
cos(newAngle) * radius;
float curY =
sin(newAngle) * radius;
[result
addObject:[NSValue valueWithCGPoint:CGPointMake(center.x + curX,center.y +
curY)]];
}
return result;
}
The method returns an array of NSValue objects that wrap CGPoints.
You can retrieve the CGPoint contents by sending the CGPointValue message to
the NSValue object:
NSValue *theValue = ... // retrieve an object from the array
CGPoint thePoint = [theValue CGPointValue];