Last active
December 18, 2015 21:59
-
-
Save briandw/5851290 to your computer and use it in GitHub Desktop.
rantlab post 1.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (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