C++中關於一個點繞另一個點旋轉


點X繞原點逆時針旋轉θ角度的操作如下:

 繞原點逆時針旋轉X的角度θ:X*polar( 1.0,θ )。

此處,複數的polar函式定義在<complex>標頭檔案中,用於使用相位角和幅值查詢複數。polar(mag,angle) 返回一個複數。

點X繞點Y旋轉

要使一個點繞另一個點旋轉,我們將使用平移,其中所有座標的移動都發生在特定方向上。

X繞Y旋轉的步驟。

  • 將X平移到Y,使Y成為新的原點。這可以透過從所有點中減去Y來完成。X現在變為X-Y。

  • 使用上述公式圍繞新原點旋轉(X-Y):(X-Y)*polar( 1.0,θ )

  • 透過向所有點新增Y進行反向平移。

X繞Y旋轉的結果為:(X-Y)*polar( 1.0,θ ) + Y

以下是演示點繞另一個點旋轉的程式碼

示例

 線上演示

#include <iostream>
#include <complex>
using namespace std;
typedef complex<double> point;
#define x real()
#define y imag()
int main(){
   // Rotate P about Q
   point X(5.0, 3.0);
   point Y(2.0, 4.0);
   // Angle of rotation is 90 degrees
   double theta = 3.14/2;
   point Xnew=(X-Y) * polar(1.0, theta) + Y;
   cout << "rotating X 90 degrees anti-clockwise about Y becomes:";
   cout << "(" << Xnew.x << ", " << Xnew.y << ")" << endl;
   return 0;
}

輸出

rotating X 90 degrees anti-clockwise about Y becomes:(3.00239, 6.9992)

更新於: 2020-07-28

959 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.