getting a point from an array of NSValue CGPoints
getting a point from an array of NSValue CGPoints
- Subject: getting a point from an array of NSValue CGPoints
- From: Jeff Decker <email@hidden>
- Date: Fri, 22 May 2009 15:45:30 -0400
Hello,
Thank you for your help. I have a mutable array filled with NSValues
which are CGPoints. I want to unpack them one by one while adding
them to a CGContextAddLineToPoint rect. Here is my terrible attempt
(most of the code is from the Stanford class on iTunes - which I'm
really enjoying!):
- (void)drawRect:(CGRect)rect {
NSLog(@"drawRect called");
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor grayColor] set];
UIRectFill ([self bounds]);
NSArray *arrayOfPoints = [PolygonView pointsForPolygonInRect:rect
numberOfSides:[myPolygon numberOfSides]];
NSLog(@"returning to drawRect from pointsForPolygonInRect");
CGPoint center = CGPointMake(rect.size.width / 2.0,
rect.size.height / 2.0);
CGContextBeginPath (context);
CGContextMoveToPoint (context, center.x, center.y);
NSLog(@"Context just moved to center: %f, %f", center.x,
center.y); //log shows expected values
for (int i = 0; i < [myPolygon numberOfSides]; i++){
NSValue *value = [arrayOfPoints objectAtIndex:i]; //yikes!!!
CGPoint point = [value CGPointValue]; //yikes!!!
NSLog(@"points plotted: %f, %f", point.x, point.y);
CGContextAddLineToPoint (context, point.x, point.y);
}
CGContextClosePath (context);
[[UIColor redColor] setFill];
[[UIColor blackColor] setStroke];
CGContextDrawPath (context, kCGPathFillStroke);
}
+ (NSArray *)pointsForPolygonInRect:(CGRect)rect numberOfSides:
(int)numberOfSides {
NSLog(@"pointsForPolygonInRect called");
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;
}
Thanks,
Jeff
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden