計算一個點圍繞另一個點旋轉指定弧度后坐標值的方法


1.示例圖

P(x1,y1)以點A(a,b)為圓心,旋轉弧度為θ,求旋轉后點Q(x2,y2)的坐標

 

 

2.實現方法

先將坐標平移,計算點(x1-a,y1-b)圍繞原點旋轉后的坐標,再將坐標軸平移到原狀態

/// <summary>
/// 結構:表示一個點
/// </summary>
struct Point
{
 //橫、縱坐標
 public double x, y;
 //構造函數
 public Point(double x, double y)
 {
  this.x = x;
  this.y = y;
 }
 //該點到指定點pTarget的距離
 public double DistanceTo(Point p)
 {
  return Math.Sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y));
 }
 //重寫ToString方法
 public override string ToString()
 {
  return string.Concat("Point (",
   this.x.ToString("#0.000"), ',',
   this.y.ToString("#0.000"), ')');
 }
}
/// <summary>
/// 計算點P(x,y)與X軸正方向的夾角
/// </summary>
/// <param name="x">橫坐標</param>
/// <param name="y">縱坐標</param>
/// <returns>夾角弧度</returns>
private static double radPOX(double x,double y)
{
 //P在(0,0)的情況
 if (x == 0 && y == 0) return 0;
 //P在四個坐標軸上的情況:x正、x負、y正、y負
 if (y == 0 && x > 0) return 0;
 if (y == 0 && x < 0) return Math.PI;
 if (x == 0 && y > 0) return Math.PI / 2;
 if (x == 0 && y < 0) return Math.PI / 2 * 3;
 //點在第一、二、三、四象限時的情況
 if (x > 0 && y > 0) return Math.Atan(y / x);
 if (x < 0 && y > 0) return Math.PI - Math.Atan(y / -x);
 if (x < 0 && y < 0) return Math.PI + Math.Atan(-y / -x);
 if (x > 0 && y < 0) return Math.PI * 2 - Math.Atan(-y / x);
 return 0;
}
/// <summary>
/// 返回點P圍繞點A旋轉弧度rad后的坐標
/// </summary>
/// <param name="P">待旋轉點坐標</param>
/// <param name="A">旋轉中心坐標</param>
/// <param name="rad">旋轉弧度</param>
/// <param name="isClockwise">true:順時針/false:逆時針</param>
/// <returns>旋轉后坐標</returns>
private static Point RotatePoint(Point P, Point A, 
 double rad, bool isClockwise = true)
{
 //點Temp1
 Point Temp1 = new Point(P.x - A.x, P.y - A.y);
 //點Temp1到原點的長度
 double lenO2Temp1 = Temp1.DistanceTo(new Point(0, 0));
 //∠T1OX弧度
 double angT1OX = radPOX(Temp1.x, Temp1.y);
 //∠T2OX弧度(T2為T1以O為圓心旋轉弧度rad)
 double angT2OX = angT1OX - (isClockwise ? 1 : -1) * rad;
 //點Temp2
 Point Temp2 = new Point(
  lenO2Temp1 * Math.Cos(angT2OX),
  lenO2Temp1 * Math.Sin(angT2OX));
 //點Q
 return new Point(Temp2.x + A.x, Temp2.y + A.y);
}

 


.Main函數調用
static void Main(string[] args)
{
 //求兩點間長度
 Point A = new Point(0, 0);
 Point B = new Point(3, 4);
 Console.WriteLine("Length of AB: " + A.DistanceTo(B));
 Point P = new Point(5, -5);
 Console.WriteLine(P.ToString() + '\n');
 //繞原點(0,0)逆時針旋轉
 Console.WriteLine(RotatePoint(P, new Point(0, 0), Math.PI / 4 * 9, false));
 Console.WriteLine(RotatePoint(P, new Point(0, 0), Math.PI / 4 * 10, false));
 Console.WriteLine(RotatePoint(P, new Point(0, 0), Math.PI / 4 * 11, false));
 Console.WriteLine(RotatePoint(P, new Point(0, 0), Math.PI / 4 * 12, false));
 Console.WriteLine(RotatePoint(P, new Point(0, 0), Math.PI / 4 * 13, false));
 Console.WriteLine(RotatePoint(P, new Point(0, 0), Math.PI / 4 * 14, false));
 Console.WriteLine(RotatePoint(P, new Point(0, 0), Math.PI / 4 * 15, false));
 Console.WriteLine(RotatePoint(P, new Point(0, 0), Math.PI / 4 * 16, false));
 Console.WriteLine();
 //繞點(2.5,2.5)順時針旋轉
 Console.WriteLine(RotatePoint(P, new Point(2.5, 2.5), Math.PI / 4 * 1));
 Console.WriteLine(RotatePoint(P, new Point(2.5, 2.5), Math.PI / 4 * 2));
 Console.WriteLine(RotatePoint(P, new Point(2.5, 2.5), Math.PI / 4 * 3));
 Console.WriteLine(RotatePoint(P, new Point(2.5, 2.5), Math.PI / 4 * 4));
 Console.WriteLine(RotatePoint(P, new Point(2.5, 2.5), Math.PI / 4 * 5));
 Console.WriteLine(RotatePoint(P, new Point(2.5, 2.5), Math.PI / 4 * 6));
 Console.WriteLine(RotatePoint(P, new Point(2.5, 2.5), Math.PI / 4 * 7));
 Console.WriteLine(RotatePoint(P, new Point(2.5, 2.5), Math.PI / 4 * 8));
 Console.ReadLine();
}

 

4.運行結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM