如何旋转某个点周围的顶点? [英] How to rotate a vertex around a certain point?
问题描述
float distX = Math.abs(centerX-point2X);
float distY = Math.abs(centerY-point2Y);
float dist = FloatMath.sqrt(distX * distX + distY * distY);
到目前为止,我只是找到了两点之间的距离......任何想法我应该在哪里去那里?
最简单的方法是编写三个转换:
- 将点1带到原点的翻译
- 围绕原点旋转所需角度
- 带来点1的翻译回到原来的位置
当您完成所有工作时,您将得到以下转换结果:
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)
的条款来颠倒标志。
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:
- A translation that brings point 1 to the origin
- Rotation around the origin by the required angle
- A translation that brings point 1 back to its original position
When you work this all out, you end up with the following transformation:
newX = centerX + (point2x-centerX)*Math.cos(x) - (point2y-centerY)*Math.sin(x);
newY = centerY + (point2x-centerX)*Math.sin(x) + (point2y-centerY)*Math.cos(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屋!