Optimize a views drawing code

Posted by xon1c on Stack Overflow See other posts from Stack Overflow or by xon1c
Published on 2010-05-26T09:30:39Z Indexed on 2010/05/26 10:31 UTC
Read the original article Hit count: 209

Filed under:
|
|
|
|

Hi,

in a simple drawing application I have a model which has a NSMutableArray curvedPaths holding all the lines the user has drawn. A line itself is also a NSMutableArray, containing the point objects. As I draw curved NSBezier paths, my point array has the following structure: linePoint, controlPoint, controlPoint, linePoint, controlPoint, controlPoint, etc... I thought having one array holding all the points plus control points would be more efficient than dealing with 2 or 3 different arrays.

Obviously my view draws the paths it gets from the model, which leads to the actual question: Is there a way to optimize the following code (inside the view's drawRect method) in terms of speed?

int lineCount = [[model curvedPaths] count];

// Go through paths
for (int i=0; i < lineCount; i++)
{
    // Get the Color
    NSColor *theColor = [model getColorOfPath:[[model curvedPaths] objectAtIndex:i]];

    // Get the points
    NSArray *thePoints = [model getPointsOfPath:[[model curvedPaths] objectAtIndex:i]];

    // Create a new path for performance reasons
    NSBezierPath *path = [[NSBezierPath alloc] init];

    // Set the color
    [theColor set];

    // Move to first point without drawing
    [path moveToPoint:[[thePoints objectAtIndex:0] myNSPoint]];

    int pointCount = [thePoints count] - 3;

    // Go through points
    for (int j=0; j < pointCount; j+=3)
    {
        [path curveToPoint:[[thePoints objectAtIndex:j+3] myNSPoint] 
             controlPoint1:[[thePoints objectAtIndex:j+1] myNSPoint]
             controlPoint2:[[thePoints objectAtIndex:j+2] myNSPoint]];
    }

    // Draw the path
    [path stroke];

    // Bye stuff
    [path release];
    [theColor release];
}

Thanks, xonic

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa