Skip to content

Instantly share code, notes, and snippets.

@briandw
Last active Dec 18, 2015
Embed
What would you like to do?
rantlab post 1.1
+ (CGPathRef )createPathWithArrowFromPoint:(CGPoint)startPoint
toPoint:(CGPoint)endPoint
tailWidth:(CGFloat)tailWidth
headWidth:(CGFloat)headWidth
headLength:(CGFloat)headLength
wiggle:(CGFloat)wiggle
{
CGFloat length = hypotf(endPoint.x - startPoint.x, endPoint.y - startPoint.y);
CGPoint points[9];
CGFloat deflection = wiggle*length/4.0;
CGFloat tailLength = length - headLength;
CGFloat halfTail = tailWidth/2.0;
points[0] = CGPointMake(0, halfTail);
points[1] = CGPointMake(tailLength/2, deflection + halfTail);
points[2] = CGPointMake(tailLength, halfTail);
points[3] = CGPointMake(tailLength, headWidth / 2);
points[4] = CGPointMake(length, 0);
points[5] = CGPointMake(tailLength, -headWidth / 2);
points[6] = CGPointMake(tailLength, -halfTail);
points[7] = CGPointMake(tailLength/2, -halfTail + deflection);
points[8] = CGPointMake(0, -halfTail);
CGAffineTransform transform = [self transformForStartPoint:startPoint
endPoint:endPoint
length:length];
CGMutablePathRef cgPath = CGPathCreateMutable();
CGPathMoveToPoint(cgPath, &transform, points[0].x, points[0].y);
CGPathAddQuadCurveToPoint(cgPath, &transform, points[1].x, points[1].y, points[2].x, points[2].y);
CGPathAddLineToPoint(cgPath, &transform, points[3].x, points[3].y);
CGPathAddLineToPoint(cgPath, &transform, points[4].x, points[4].y);
CGPathAddLineToPoint(cgPath, &transform, points[5].x, points[5].y);
CGPathAddLineToPoint(cgPath, &transform, points[6].x, points[6].y);
CGPathAddQuadCurveToPoint(cgPath, &transform, points[7].x, points[7].y, points[8].x, points[8].y);
CGPathCloseSubpath(cgPath);
return cgPath;
}
+ (CGAffineTransform)transformForStartPoint:(CGPoint)startPoint
endPoint:(CGPoint)endPoint
length:(CGFloat)length
{
CGFloat cosine = (endPoint.x - startPoint.x) / length;
CGFloat sine = (endPoint.y - startPoint.y) / length;
return (CGAffineTransform){ cosine, sine, -sine, cosine, startPoint.x, startPoint.y };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment