Created
August 17, 2011 09:54
-
-
Save hatfinch/1151231 to your computer and use it in GitHub Desktop.
CGAffineTransformFromRectToRect (not working)
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 CGAffineTransformFromRectToRect(CGRect fromRect, CGRect toRect) | |
{ | |
CGSize scale = CGSizeMake(toRect.size.width / fromRect.size.width, toRect.size.height / fromRect.size.height); | |
CGRect scaledFromRect = CGRectMake(fromRect.origin.x * scale.width, fromRect.origin.y * scale.height, | |
fromRect.size.width * scale.width, fromRect.size.height * scale.height); | |
CGSize translation = CGSizeMake(fromRect.origin.x - scaledFromRect.origin.x, fromRect.origin.y - scaledFromRect.origin.y); | |
return CGAffineTransformMake(scale.width, 0.0, 0.0, scale.height, translation.width, translation.height); | |
} | |
CGRect fromRect = CGRectMake(3, -1, 1, 1); | |
CGRect toRect = CGRectMake(1, 3, 3, 2); | |
CGAffineTransform trans = OLVAffineTransformFromRectToRect(fromRect, toRect); // [3, 0, 0, 2, -6, 1] | |
CGRect calculatedFromRect = CGRectApplyAffineTransform(fromRect, trans); // {{3, -1}, {3, 2}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
improved @MattFoley variant (fix for non-zero initial coordinates)