CGRectApplyAffineTransform 然后 CGAffineTransformInvert 不会返回到原始位置

发布于 2024-12-07 04:16:44 字数 1788 浏览 3 评论 0原文

这似乎不可能,我确信有一个简单的解决方案,但我无法解决。我的高中矩阵数学有点生疏:)

我正在构建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪歌 2024-12-14 04:16:44

复制自 文档
您可以通过调用函数 CGRectApplyAffineTransform 来操作 CGRect 结构。此函数返回包含传递给它的矩形的变换角点的最小矩形。如果对矩形进行仿射变换仅执行缩放和平移操作,则返回的矩形与由四个变换角构造的矩形重合。

Copied from documentation:
You can operate on a CGRect structure by calling the function CGRectApplyAffineTransform. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文