To get the perpendicular, you can take the vector pointing in the direction of the last line segment, then swap x and y, and invert one of them. Something like this:
CGPoint v = { currentPoint.x - lastPoint.x, currentPoint.y - lastPoint.y };
CGPoint perp;
if (v.x == 0)
{
perp.x = -v.y;
perp.y = v.x;
}
else
{
perp.x = v.y;
perp.y = -v.x;
}
Now you can draw a line in the direction of perp
, starting at the current point, like this:
CGPathMoveToPoint (somePath, currentPoint);
CGPathAddLineToPoint (somePath, NULL, currentPoint.x + perp.x * length, currentPoint.y + perp.y * length);
where length
is the length of the line segment you want to draw.
And don't forget to set lastPoint
to currentPoint
so it's correct the next time around:
lastPoint = currentPoint;