当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现计算一个点围绕另一个点旋转指定弧度后坐标值的方法

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

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网