如何围绕某个点旋转顶点? [英] How to rotate a vertex around a certain point?
问题描述
假设您在二维空间中有两个点,您需要将其中一个点旋转 X 度,另一个点作为中心.
Imagine you have two points in 2d space and you need to rotate one of these points by X degrees with the other point acting as a center.
float distX = Math.abs( centerX -point2X );
float distY = Math.abs( centerY -point2Y );
float dist = FloatMath.sqrt( distX*distX + distY*distY );
到目前为止,我只是找到了两点之间的距离……我应该从哪里开始有任何想法?
So far I just got to finding the distance between the two points... any ideas where should I go from that?
推荐答案
最简单的方法是组合三个转换:
The easiest approach is to compose three transformations:
- 将第 1 点带到原点的翻译
- 绕原点旋转所需的角度
- 将第 1 点带回其原始位置的翻译
当你把这一切都搞定后,你会得到以下转换(其中 x
是所需的旋转角度,以弧度为单位):
When you work this all out, you end up with the following transformation (where x
is the desired angle of rotation in radians):
newX = centerX + (point2x-centerX)*Math.cos(x) - (point2y-centerY)*Math.sin(x);
newY = centerY + (point2x-centerX)*Math.sin(x) + (point2y-centerY)*Math.cos(x);
请注意,这假设角度 x
对于顺时针旋转为负(所谓的 坐标系的标准或右手方向).如果情况并非如此,则您需要反转涉及 sin(x)
的术语的符号.
Note that this makes the assumption that the angle x
is negative for clockwise rotation (the so-called standard or right-hand orientation for the coordinate system). If that's not the case, then you would need to reverse the sign on the terms involving sin(x)
.
这篇关于如何围绕某个点旋转顶点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!