PopKernel
PopKernel

Reputation: 4270

Quartz 2D jagged lines

Check out this weird path my paint application produces:

/Users/michaelsavich/Desktop/Debug pic.jpeg

The red line in the middle is a debug feature which represents the input mouse movements and the distance between them, at 1 px line width, and the black is the output, at 30 px width.

The way I'm drawing this is simple, just point-to-point between the coalesced mouse events:

//This is called in mouseDragged: and [theEvent locationInWindow] is passed to point
-(void)addPointToCurrentStroke:(CGPoint)point 

if (self.newStroke == TRUE) //If the currentStroke doesn't exist or is empty
{
   self.currentStroke = CGPathCreateMutable();
   CGPathMoveToPoint(self.currentStroke, NULL, point.x, point.y);
   self.newStroke = FALSE;
}

else if (self.newStroke == FALSE)
    CGPathAddLineToPoint(self.currentStroke, NULL, point.x, point.y);
}

This path is then stroked in drawRect: with the appropriate user settings, in this case size 30 and color black. Note the round line cap, which makes it even weirder that it juts out like that.

So why am I getting those spikes poking so far out from the intended path? I can't make heads or tails of it. If anyone could tell me what's going on here, I would greatly appreciate it.

Upvotes: 0

Views: 76

Answers (1)

Kurt Revis
Kurt Revis

Reputation: 27984

Your CG context is set to use a miter join at each corner of the path. When there's a small angle at that corner, the miter can stick out a significant distance. Use CGContextSetLineJoin to specify a round join or bevel join instead.

See the Quartz 2D Programming Guide, "Parameters That Affect Stroking".

Upvotes: 1

Related Questions