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

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

问题描述

如何仅使用一次调用 CGAffineTransformMake() 加上 math.h 触发函数,如 sin()、cos() 等,进行 Core Graphics 仿射变换以围绕角度 a 的点 x,y 旋转.,并且没有其他 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.

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

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 对应于仿射变换:

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.

附言我还想提醒读到这里的任何人,affine"在ask"中的发音是短的a",而不是able"中的长a".我什至听到 Apple 员工在他们的 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天全站免登陆