C#实现计算一个点围绕另一个点旋转指定弧度后坐标值的方法

时间:2021-05-20

本文实例讲述了C#实现计算一个点围绕另一个点旋转指定弧度后坐标值的方法。分享给大家供大家参考。具体如下:

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);}

3.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.运行结果:

希望本文所述对大家的C#程序设计有所帮助。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章