如何旋转某个点周围的顶点? [英] How to rotate a vertex around a certain point?

查看:129
本文介绍了如何旋转某个点周围的顶点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你在二维空间中有两个点,你需要将这些点中的一个旋转X度,另一个点作为中心点。

  float distX = Math.abs(centerX-point2X); 
float distY = Math.abs(centerY-point2Y);

float dist = FloatMath.sqrt(distX * distX + distY * distY);

到目前为止,我只是找到了两点之间的距离......任何想法我应该在哪里去那里?



解决方案

最简单的方法是编写三个转换:


  1. 将点1带到原点的翻译
  2. 围绕原点旋转所需角度​​
  3. 带来点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:

  1. A translation that brings point 1 to the origin
  2. Rotation around the origin by the required angle
  3. 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屋!

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