Skip to content

Instantly share code, notes, and snippets.

@jjrscott
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjrscott/8759756a69d3f93ce11b to your computer and use it in GitHub Desktop.
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.
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