绕一点旋转的一步仿射变换? [英] One step affine transform for rotation around a point?

查看:109
本文介绍了绕一点旋转的一步仿射变换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用CGAffineTransformMake()加上math.h触发函数(如sin(),cos()等)对围绕角度a的x,y点旋转进行Core Graphics仿射变换。 。,而且没有其他CG电话。

How can I make a Core Graphics affine transform for rotation around a point x,y of angle a, using only a single call to CGAffineTransformMake() plus math.h trig functions such as sin(), cos(), etc., and no other CG calls.

这里的其他答案似乎是关于使用多个堆叠变换或多步变换来移动,旋转和移动,使用多个核心图形调用。这些答案不符合我的具体要求。

Other answers here seem to be about using multiple stacked transforms or multi-step transforms to move, rotate and move, using multiple Core Graphics calls. Those answers do not meet my specific requirements.

推荐答案

围绕点(x,y)的角度旋转对应于仿射变换:

A rotation of angle a around the point (x,y) corresponds to the affine transformation:

CGAffineTransform transform = CGAffineTransformMake(cos(a),sin(a),-sin(a),cos(a),x-x*cos(a)+y*sin(a),y-x*sin(a)-y*cos(a));

您可能需要插入-a而不是a,具体取决于您是否要将旋转顺时针旋转或逆时针。此外,您可能需要插入-y而不是y,具体取决于您的坐标系是否颠倒。

You may need to plug in -a instead of a depending on whether you want the rotation to be clockwise or counterclockwise. Also, you may need to plug in -y instead of y depending on whether or not your coordinate system is upside down.

此外,您可以完成相同的操作三行代码使用:

Also, you can accomplish precisely the same thing in three lines of code using:

CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
transform = CGAffineTransformRotate(transform, a);
transform = CGAffineTransformTranslate(transform,-x,-y);

如果您将此应用于视图,您还可以通过CGAffineTransformMakeRotation简单地使用旋转变换(a ),前提是您设置视图的图层的anchorPoint属性以反映您想要旋转的点。但是,听起来你似乎对将它应用于视图不感兴趣。

If you were applying this to a view, you could also simply use a rotation transform via CGAffineTransformMakeRotation(a), provided you set the view's layer's anchorPoint property to reflect the point you want to rotate around. However, is sounds like you aren't interested in applying this to a view.

最后,如果你将它应用于非欧几里德2D空间,你可能不会想要一个仿射变革。仿射变换是欧几里德空间的等距,意味着它们保持标准的欧几里德距离以及角度。如果你的空间不是欧几里德,那么你想要的变换实际上可能不是仿射,或者如果它是仿射的,那么旋转的矩阵可能不像我上面用sin和cos写的那么简单。例如,如果你处于双曲线空间,你可能需要使用双曲线触发函数sinh和cosh,以及公式中不同的+和 - 符号。

Finally, if you are applying this to a non-Euclidean 2D space, you may not want an affine transformation at all. Affine transformations are isometries of Euclidean space, meaning that they preserve the standard Euclidean distance, as well as angles. If your space is not Euclidean, then the transformation you want may not actually be affine, or if it is affine, the matrix for the rotation might not be as simple as what I wrote above with sin and cos. For instance, if you were in a hyperbolic space, you might need to use the hyperbolic trig functions sinh and cosh, along with different + and - signs in the formula.

PS我还想提醒任何读到这篇文章的人,affine的发音是短的a,如ask,而不是a,如able。我甚至听说过苹果公司员工在WWDC会谈中误解了它。

P.S. I also wanted to remind anyone reading this far that "affine" is pronounced with a short "a" as in "ask", not a long "a" as in "able". I have even heard Apple employees mispronouncing it in their WWDC talks.

这篇关于绕一点旋转的一步仿射变换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆