Last active
August 29, 2015 14:06
-
-
Save jjrscott/8759756a69d3f93ce11b to your computer and use it in GitHub Desktop.
Use two points to generate a transform which will rotate one the start point to the angle of the second.
This file contains 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
CGAffineTransform AffineTransformToPositionOnLines(const CGPoint points[], | |
size_t count, | |
CGFloat position, | |
BOOL rotate) | |
{ | |
CGAffineTransform transform = CGAffineTransformIdentity; | |
if (count > 1) | |
{ | |
for (size_t index = 0;index<count-1;index++) | |
{ | |
CGFloat dx = points[index+1].x - points[index].x; | |
CGFloat dy = points[index+1].y - points[index].y; | |
CGFloat dh = hypot(dx, dy); | |
if (position < dh) | |
{ | |
CGFloat cosA = dx/dh; | |
CGFloat sinA = dy/dh; | |
transform = CGAffineTransformTranslate(transform, points[index].x, points[index].y); | |
CGFloat fd = position / dh; | |
transform = CGAffineTransformTranslate(transform, dx * fd, dy * fd); | |
if (rotate) | |
{ | |
transform = CGAffineTransformConcat(CGAffineTransformMake(cosA, sinA, -sinA, cosA, 0, 0), transform); | |
} | |
break; | |
} | |
position -= dh; | |
} | |
} | |
return transform; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment