CGRectApplyAffineTransform 然后 CGAffineTransformInvert 不会返回到原始位置
这似乎不可能,我确信有一个简单的解决方案,但我无法解决。我的高中矩阵数学有点生疏:)
我正在构建一个 CGAffineTransform
来对 CGRect
进行平移-旋转-平移,然后尝试使用逆 CGAffineTransformInvert 返回到原始位置。但事实并非如此。我已设法将其简化为:
// start building the transform...
// translate so (0,0) is at centre of image
CGAffineTransform affine = CGAffineTransformMakeTranslation(-100, -100);
// add a rotate
affine = CGAffineTransformConcat(affine, CGAffineTransformMakeRotation(M_PI/4));
// get the inverse
CGAffineTransform inverseAffine = CGAffineTransformInvert(affine);
// now test this: pick a starting CGRect
CGRect start = CGRectMake(50, 40, 10, 20);
// use my CGAffineTransform to move "start" to "midpoint"
CGRect midpoint = CGRectApplyAffineTransform(start, affine);
// use the inverse to move "midpoint" to "finish"
CGRect finish = CGRectApplyAffineTransform(midpoint, inverseAffine);
// what did we get
NSLog(@"start %5.1f %5.1f %5.1f %5.1f", start.origin.x, start.origin.y, start.size.width, start.size.height);
NSLog(@"midpoint %5.1f %5.1f %5.1f %5.1f", midpoint.origin.x, midpoint.origin.y, midpoint.size.width, midpoint.size.height);
NSLog(@"finish %5.1f %5.1f %5.1f %5.1f", finish.origin.x, finish.origin.y, finish.size.width, finish.size.height);
输出如下所示:
2011-09-29 ... start 50.0 40.0 10.0 20.0
2011-09-29 ... midpoint -7.1 -77.8 21.2 21.2
2011-09-29 ... finish 40.0 35.0 30.0 30.0
结束矩形不应该与开始矩形相同吗?这不是 CGAffineTransformInvert
应该做的吗?
如果我做同样的事情,但使用 CGPoint
而不是 CGRect
,它似乎工作正常。
This does not seem possible and I'm sure there's a simple solution but I cannot work it out. My High School matrix math is a bit rusty :)
I'm building a CGAffineTransform
to do translation-rotation-translation of a CGRect
and then trying to use the inverse CGAffineTransformInvert to return to the original location. But it doesn't. I've managed to reduce it to this:
// start building the transform...
// translate so (0,0) is at centre of image
CGAffineTransform affine = CGAffineTransformMakeTranslation(-100, -100);
// add a rotate
affine = CGAffineTransformConcat(affine, CGAffineTransformMakeRotation(M_PI/4));
// get the inverse
CGAffineTransform inverseAffine = CGAffineTransformInvert(affine);
// now test this: pick a starting CGRect
CGRect start = CGRectMake(50, 40, 10, 20);
// use my CGAffineTransform to move "start" to "midpoint"
CGRect midpoint = CGRectApplyAffineTransform(start, affine);
// use the inverse to move "midpoint" to "finish"
CGRect finish = CGRectApplyAffineTransform(midpoint, inverseAffine);
// what did we get
NSLog(@"start %5.1f %5.1f %5.1f %5.1f", start.origin.x, start.origin.y, start.size.width, start.size.height);
NSLog(@"midpoint %5.1f %5.1f %5.1f %5.1f", midpoint.origin.x, midpoint.origin.y, midpoint.size.width, midpoint.size.height);
NSLog(@"finish %5.1f %5.1f %5.1f %5.1f", finish.origin.x, finish.origin.y, finish.size.width, finish.size.height);
The output looks like this:
2011-09-29 ... start 50.0 40.0 10.0 20.0
2011-09-29 ... midpoint -7.1 -77.8 21.2 21.2
2011-09-29 ... finish 40.0 35.0 30.0 30.0
Shouldn't the finish rect be the same as the start? Isn't that what CGAffineTransformInvert
is supposed to do?
If I do the same thing but with a CGPoint
instead of a CGRect
it seems to work ok.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
复制自 文档:
您可以通过调用函数
CGRectApplyAffineTransform
来操作CGRect
结构。此函数返回包含传递给它的矩形的变换角点的最小矩形。如果对矩形进行仿射变换仅执行缩放和平移操作,则返回的矩形与由四个变换角构造的矩形重合。Copied from documentation:
You can operate on a
CGRect
structure by calling the functionCGRectApplyAffineTransform
. This function returns the smallest rectangle that contains the transformed corner points of the rectangle passed to it. If the affine transform that operates on the rectangle performs only scaling and translation operations, the returned rectangle coincides with the rectangle constructed from the four transformed corners.好吧,我发现了我的错误。
不是一些奇怪的扭曲的非 CGRect 风格的东西。
OK I found my mistake.
not some strange twisted non-CGRect-ish thing.