📌  相关文章
📜  在 C++ 中一个点关于另一个点的旋转

📅  最后修改于: 2021-10-23 08:55:36             🧑  作者: Mango

我们已经讨论了集合 1 和集合 2 中点 P 绕原点的旋转。 点 P 绕原点沿逆时针方向旋转角度为 θ 的情况如下:

Rotation of P about origin: P * polar(1.0, θ)

P 绕点 Q 旋转

现在,我们必须不围绕原点而是围绕一般点 Q 旋转点 P。这可以通过平移方法很容易理解,平移方法是几何分析中采用的一种非常常用的技术。
什么是翻译?
在欧几里得几何中,平移是一种几何变换,它使图形或空间的每个点在给定方向上移动相同的量。

如何进行翻译?
平移也可以解释为向每个点添加一个常数向量,或者平移坐标系的原点。
平移后,进行所需的计算,并通过减去每个点的常数向量或将原点移回原点来使平移无效。

坐标轴的平移

因此,为了使 P 绕 Q 旋转,我们在 Q 处移动原点,即我们从坐标平面的每个点减去 Q 的等效矢量。现在新点 P – Q 必须绕原点旋转,然后平移必须无效。
这些步骤可以描述如下:

  1. 平移(在 Q 处移动原点):从所有点中减去 Q。因此,P 变为 P – Q
  2. (P – Q) 关于原点的旋转: (P – Q) * polar(1.0, θ)
  3. 恢复原点:将 Q 添加到所有点。

因此,

Rotation of P about Q : (P – Q) * polar(1.0, θ) + Q
// CPP example to illustrate the rotation 
// of a point about another point
#include 
#include 
  
using namespace std;
  
typedef complex point;
#define x real()
#define y imag()
  
// Constant PI for providing angles in radians
#define PI 3.1415926535897932384626
  
// Function used to display X and Y coordinates of a point
void displayPoint(point P)
{
    cout << "(" << P.x << ", " << P.y << ")" << endl;
}
  
//Function for Rotation of P about Q by angle theta
point rotate(point P, point Q, double theta)
{
    return (P-Q) * polar(1.0, theta) +  Q;
}
  
int main()
{
    // Rotate P about Q
    point P(4.0, 3.0);
    point Q(2.0, 2.0);
  
    // Angle of rotation = 90 degrees
    double theta = PI/2;
      
    point P_rotated = rotate(P, Q, theta);
    cout << "The point P on rotating 90 degrees anti-clockwise about Q becomes:";
    cout << "P_rotated"; displayPoint(P_rotated);
  
    return 0;
}

输出:

The point P on rotating 90 degrees anti-clockwise about Q becomes: P_rotated(1, 4)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程。