如何从 QTransform 中提取旋转角度?
我有一个 QTransform 对象,想知道该对象旋转的角度(以度为单位),但是没有明确的示例说明如何执行此操作:
http://doc.trolltech.com/4.4/qtransform.html#basic-matrix-operations
设置它很容易,将其恢复出来再次很难。
I have a QTransform object and would like to know the angle in degrees that the object is rotated by, however there is no clear example of how to do this:
http://doc.trolltech.com/4.4/qtransform.html#basic-matrix-operations
Setting it is easy, getting it back out again is hard.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设变换仅包含旋转,这很简单:只需取 m11 元素的 acos 即可。
如果变换包含平移,它仍然有效,但如果它包含剪切或缩放,那么你就不走运了。 这些可以通过将矩阵分解为剪切、缩放和旋转矩阵来重建,但您得到的结果很可能不是您想要的。
Assuming, that the transform ONLY contains a rotation it's easy: Just take the acos of the m11 element.
It still works if the transform contains a translation, but if it contains shearing or scaling you're out of luck. These can be reconstructed by decomposing the matrix into a shear, scale and rotate matrix, but the results you get aren't most likely what you're looking for.
最简单的一般方式就是对(0,0)和(1,0)进行变换,然后用三角函数(反正切)得到角度
The simplest general way is to transform (0,0) and (1,0), then use trigonometric functions (arctan) to get the angle
变换矩阵是用于 3d 图形的实现。 它简化了数学计算,以加速点/对象的 3D 位置/旋转方向。 由于变换累积连续平移/旋转/缩放的方式,确实很难从变换中提取方向。
这是一个建议。 取一个指向简单方向(如 (1,0,0))的向量,然后对其应用变换。 您得到的向量将被平移和旋转,得到如下所示的结果:(27.8, 19.2, 77.4)。 将变换应用于 (0,0,0),得到类似 (26.1, 19.4, 50.8) 的结果。 您可以使用这两个点来计算基于知道它们的起点 (1,0,0) 所应用的旋转。
这有帮助吗?
The Transformation Matrix is an implementation used for 3d graphics. It simplifies the math in order to speed up 3d positional / rotational orientations of points / objects. It is indeed very hard to pull out orientation from the Transformation because of the way it accumulates successive translations / rotations / scales.
Here's a suggestion. Take a vector that points in a simple direction like (1,0,0), and then apply the Transform to it. Your resulting vector will be translated and rotated to give you something like this: (27.8, 19.2, 77.4). Apply the Transform to (0,0,0), to get something like (26.1, 19.4, 50.8). You can use these two points to calculate the rotations that have been applied based on knowing their starting points of (1,0,0).
Does this help?
一般来说,您需要一个反三角函数,但您需要注意象限的模糊性,这就是您应该使用的 atan2 (有时拼写为 arctan2)。 因此,要么将单位向量 [0, 1] 旋转为 [x, y],然后使用 atan2(y,x),或者如果矩阵仅实现旋转,则可以使用 atan2(m12,m11)。 (这些与 Javier 和 Nils 的答案类似,只是他们不使用 atan2。)
Generally you need an inverse trig function, but you need to watch out for quadrant ambiguities, and this is what you should use atan2 (sometimes spelled arctan2). So either rotate a unit vector [0, 1] to [x, y] and then use atan2(y,x), or if the matrix is only implementing a rotation you can use atan2(m12,m11). (These are similar to Javier and Nils answers, except they don't use atan2.)
我仅将 QGraphicsItem 与 setRotate 一起使用,并且没有任何问题,直到我添加旋转组功能。 问题是,当调用 destroyItemGroup 时,它将旋转作为项目的变换而不是旋转应用。 因此,我必须从这个 QTransform 对象恢复旋转。
我的解决方法是将以下几行添加到 itemChange 方法中(归功于 tom10 的答案):
PS:使用 acos 和 m11() 的其他解决方案不起作用。 正如 tom10 所解释的,它在某些值下会崩溃。
I was using QGraphicsItem with just setRotate and wasn't having any kind of problem, until I add a rotate group functionality. The problem is that when destroyItemGroup is called, it applies the rotation as a transformation to the items, and not as a rotation. Because of that I had to recover the rotation from this QTransform object.
My fix was to add the following lines to the itemChange method (credit to tom10's answer):
PS.: The other solution with acos and m11() didn't work. It crashes for certain values, as explained by tom10.