Assignment 3 for Stanford Class on Iphone prog

Discussion in 'iOS Programming' started by TotalLuck, Jul 8, 2009.

  1. TotalLuck macrumors newbie

    TotalLuck

    Joined:
    Jan 3, 2009
    Location:
    Moreno Vallley
    #1
    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;
    }
     
  2. fallinj macrumors newbie

    Joined:
    Jul 18, 2009
    #2
    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.
     

Share This Page