views:

277

answers:

1

You can probably see where I am going with this - but is there any easy way to generate a CGAffineTransform from two lines - each represented by a pair of CGPoints:

A[(a1x,a1y), (a2x,a2y)],
B[(b1x,b1y), (b2x,b2y)]

The resultant CGAffineTransform when applied to line A would of course produce line B. It potentially could involve a translation, scale, and rotation.

Certainly I would hope to be able to write this myself after brushing up on some trig but I was wondering if anything is already available to do this?

Note: I am not asking you to write this for me - I just don't want to miss a Core Graphics trick!

+1  A: 

Yes, but there are at least four solutions: two translations and two scales (one per operation and direction). That's without counting transformations that both translate and scale.

“The Math Behind the Matrices”, from the Quartz 2D Programming Guide, is a good overview of how each operation works; from that, it should be simple enough to invert it and come up with a matrix for the desired operation and direction.

Certainly I would hope to be able to write this myself after brushing up on some trig…

You won't need that except for rotation.

Peter Hosey
Thanks for the references. However, I think my question wasn't clear enough so I have revised it. The pairs of points each represent a line so a transformation between the two could involve a rotation.
teabot
Oh, I see. Yeah, that seems possible. Scale is easy: divide lengthB by lengthA. Rotation: Take the angle of each line, then subtract one angle from the other. Translation: Take the lines' center points and subtract one from the other. The trickiest part will be putting them all together in the matrix in the correct order. Scale first, I *think*.
Peter Hosey
Thanks - that's what I'll do.
teabot