Re: getting a point from an array of NSValue CGPoints
Re: getting a point from an array of NSValue CGPoints
- Subject: Re: getting a point from an array of NSValue CGPoints
- From: Jeff Decker <email@hidden>
- Date: Sat, 23 May 2009 11:32:13 -0400
Thank you for your response. Your reply prompted me to explore where
else my problem might lay if it is not in the unpacking of CGPoint.
It turns out that the instance of my PolygonShape class in my
PolygonView class does not have any information attached to it. I am
not sure how to connect the PolygonShape instance, which is shared by
PolygonShape and Controller, to PolygonView. I declared an instance
of PolygonShape *myPolygon in the interface of PolygonView.h.
#import "PolygonView.h"
@implementation PolygonView
- (void)drawRect:(CGRect)rect {
NSLog(@"drawRect called. Number of sides: %d. %@", [myPolygon
numberOfSides], myPolygon);
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor grayColor] set];
UIRectFill ([self bounds]);
NSArray *arrayOfPoints = [PolygonView pointsForPolygonInRect:rect
numberOfSides:[myPolygon numberOfSides]];
NSLog(@"returning to drawRect from pointsForPolygonInRect. Number of
points in array now: %d", [arrayOfPoints count]);
myPolygon returns (null) in the console and numberOfSides is 0.
Logically, [arrayOfPoints count] returns 0 also.
How do I get PolygonView to "know" about the PolygonShape instance
that all the other classes know about?
Jeff Decker
Accountable Tree Service
On May 22, 2009, at 3:54 PM, David Duncan wrote:
On May 22, 2009, at 12:45 PM, Jeff Decker wrote:
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!):
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);
}
I'm not certain why the "Yikes!" here, but it looks like your doing
things just fine to me :). The only thing you might consider is
using the for-in syntax instead ala
for(NSValue *value in myPolygon) {
CGPoint point = [value CGPointValue];
...
}
But if you want to avoid packing & unpacking, then you might also
look into the CGPath APIs that allow you to do the same kinds of
construction that your doing and provides a data type that you can
add to the context and then do all the typical path stuff with.
--
David Duncan
Apple DTS Animation and Printing
_______________________________________________
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