Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TotalLuck

macrumors newbie
Original poster
Jan 3, 2009
21
0
Moreno Vallley
Hi,

I am working on the 3rd assignment form the stanford class. It is asking me to implement a Class method +(). this method finds points of a rectangle and stores them for later retrieval and use in drawing a View.

First, I am confused as to why the method is performed on the Class and not the instance of the class.

Second, would this method be called from the ObjectView Class or the Object class. I would think that if its called by the Object class it would be on the instance not the Class itself. any explanation would be really helpful.
I am inserting the supplied code.
Code:
+ (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;
}
 
Its shouldn't be an instance method because it doesn't operate on any of variables inside PolygonView instances. Its more of a utility. A helper class that can be shared by all instances of the PolygonView. That's why it should be a class method.

Also, the drawRect method you need to implement in PolygonView will call this method.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.