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

1.示例图

P(x1,y1)以点A(a,b)为圆心,旋转弧度为θ,求旋转后点Q(x2,y2)的坐标

225217_TDhn_1425762.png

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

225528_kxcZ_1425762.png

转载于:https://my.oschina.net/Tsybius2014/blog/227351

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值