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

时间:2011-09-28 15:38:38

标签: ios objective-c cgaffinetransform

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

我正在构建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做同样的事情,它似乎工作正常。

2 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:6)