Rotate a point around another point

后端 未结 2 1303
天涯浪人
天涯浪人 2020-12-01 08:09

I have a task to draw a specific graphic. As part of this task I need to rotate some dot\'s on 45 degrees.

I\'ve spent already 2 days trying to calculate a formula,

2条回答
  •  北海茫月
    2020-12-01 08:54

    You're math looks weird to me. I think dx = r*Cos(theta) and dy = r*Sin(theta).

    Here's a little program I wrote because this was bothering me, and I haven't done math is years.

    Point center = new Point() { X = 576, Y = 576 };
    
    Point previous = new Point() { X = 849, Y=561 };
    double rotation = 45;
    double rotationRadians = rotation * (Math.PI / 180);
    
    //get radius based on the previous point and r squared = a squared + b squared
    double r = Math.Sqrt(Math.Pow(previous.X - center.X, 2) + Math.Pow(previous.Y - center.Y, 2));
    Console.WriteLine("r = " + r.ToString());
    
    //calculate previous angle
    double previousAngle = Math.Atan((previous.Y - center.Y) / (previous.X - center.X));
    Console.WriteLine("Previous angle: " + previousAngle.ToString());
    
    double newAngle = previousAngle + rotationRadians;
    
    Point newP = new Point();
    newP.X = center.X + r * Math.Cos(newAngle);
    newP.Y = center.Y + r * Math.Sin(newAngle);
    
    Console.WriteLine("(" + newP.X.ToString() + ", " + newP.Y.ToString() + ")");
    
    Console.ReadLine();
    

提交回复
热议问题