欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)計(jì)算一個(gè)點(diǎn)圍繞另一個(gè)點(diǎn)旋轉(zhuǎn)指定弧度后坐標(biāo)值的方法

 更新時(shí)間:2015年08月13日 11:28:49   作者:北風(fēng)其涼  
這篇文章主要介紹了C#實(shí)現(xiàn)計(jì)算一個(gè)點(diǎn)圍繞另一個(gè)點(diǎn)旋轉(zhuǎn)指定弧度后坐標(biāo)值的方法,涉及C#針對(duì)坐標(biāo)的數(shù)學(xué)運(yùn)算相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#實(shí)現(xiàn)計(jì)算一個(gè)點(diǎn)圍繞另一個(gè)點(diǎn)旋轉(zhuǎn)指定弧度后坐標(biāo)值的方法。分享給大家供大家參考。具體如下:

1.示例圖

P(x1,y1)以點(diǎn)A(a,b)為圓心,旋轉(zhuǎn)弧度為θ,求旋轉(zhuǎn)后點(diǎn)Q(x2,y2)的坐標(biāo)

2.實(shí)現(xiàn)方法

先將坐標(biāo)平移,計(jì)算點(diǎn)(x1-a,y1-b)圍繞原點(diǎn)旋轉(zhuǎn)后的坐標(biāo),再將坐標(biāo)軸平移到原狀態(tài)

/// <summary>
/// 結(jié)構(gòu):表示一個(gè)點(diǎn)
/// </summary>
struct Point
{
 //橫、縱坐標(biāo)
 public double x, y;
 //構(gòu)造函數(shù)
 public Point(double x, double y)
 {
  this.x = x;
  this.y = y;
 }
 //該點(diǎn)到指定點(diǎn)pTarget的距離
 public double DistanceTo(Point p)
 {
  return Math.Sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y));
 }
 //重寫(xiě)ToString方法
 public override string ToString()
 {
  return string.Concat("Point (",
   this.x.ToString("#0.000"), ',',
   this.y.ToString("#0.000"), ')');
 }
}
/// <summary>
/// 計(jì)算點(diǎn)P(x,y)與X軸正方向的夾角
/// </summary>
/// <param name="x">橫坐標(biāo)</param>
/// <param name="y">縱坐標(biāo)</param>
/// <returns>夾角弧度</returns>
private static double radPOX(double x,double y)
{
 //P在(0,0)的情況
 if (x == 0 && y == 0) return 0;
 //P在四個(gè)坐標(biāo)軸上的情況:x正、x負(fù)、y正、y負(fù)
 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;
 //點(diǎn)在第一、二、三、四象限時(shí)的情況
 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>
/// 返回點(diǎn)P圍繞點(diǎn)A旋轉(zhuǎn)弧度rad后的坐標(biāo)
/// </summary>
/// <param name="P">待旋轉(zhuǎn)點(diǎn)坐標(biāo)</param>
/// <param name="A">旋轉(zhuǎn)中心坐標(biāo)</param>
/// <param name="rad">旋轉(zhuǎn)弧度</param>
/// <param name="isClockwise">true:順時(shí)針/false:逆時(shí)針</param>
/// <returns>旋轉(zhuǎn)后坐標(biāo)</returns>
private static Point RotatePoint(Point P, Point A, 
 double rad, bool isClockwise = true)
{
 //點(diǎn)Temp1
 Point Temp1 = new Point(P.x - A.x, P.y - A.y);
 //點(diǎn)Temp1到原點(diǎn)的長(zhǎng)度
 double lenO2Temp1 = Temp1.DistanceTo(new Point(0, 0));
 //∠T1OX弧度
 double angT1OX = radPOX(Temp1.x, Temp1.y);
 //∠T2OX弧度(T2為T(mén)1以O(shè)為圓心旋轉(zhuǎn)弧度rad)
 double angT2OX = angT1OX - (isClockwise ? 1 : -1) * rad;
 //點(diǎn)Temp2
 Point Temp2 = new Point(
  lenO2Temp1 * Math.Cos(angT2OX),
  lenO2Temp1 * Math.Sin(angT2OX));
 //點(diǎn)Q
 return new Point(Temp2.x + A.x, Temp2.y + A.y);
}

3.Main函數(shù)調(diào)用

static void Main(string[] args)
{
 //求兩點(diǎn)間長(zhǎng)度
 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');
 //繞原點(diǎn)(0,0)逆時(shí)針旋轉(zhuǎn)
 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();
 //繞點(diǎn)(2.5,2.5)順時(shí)針旋轉(zhuǎn)
 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.運(yùn)行結(jié)果:

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論