C# 围绕另一个点旋转一个点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13695317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:27:01  来源:igfitidea点击:

Rotate a point around another point

c#rotationgditrigonometry

提问by Vlad Spreys

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

我有一个任务来绘制一个特定的图形。作为这项任务的一部分,我需要将一些点旋转 45 度。

I've spent already 2 days trying to calculate a formula, but just couldn't get it right. I've been searching all over the place including this particular website, I'm getting very close, but I'm still not there.

我已经花了 2 天时间试图计算一个公式,但无法正确计算。我一直在到处搜索,包括这个特定的网站,我已经很接近了,但我仍然不在那里。

Here it is: I need to draw 4 different points

这是:我需要绘制 4 个不同的点

I have a specific formula to calculate there position, which is out of scope of the question, but here is what I'm getting as a result of it:

我有一个特定的公式来计算那里的位置,这超出了问题的范围,但这是我得到的结果:

int radius = 576;
int diameter = radius * 2;
Point blueA = new Point(561, 273);
Point greenB = new Point(273, 561);
Point yellowC = new Point (849, 561);
Point redD = new Point (561, 849);

result

结果

Now I need to rotate this dots on 45 degrees. I use the following code to achieve it:

现在我需要将这些点旋转 45 度。我使用以下代码来实现它:

double rotationAngle = 45;
double rotationRadians = rotationAngle * (Math.PI / 180);
int center = radius;    
result.X = (int)(Math.Cos(rotationRadians) * ((double)result.X - (double)center) - (double)Math.Sin(rotationRadians) * ((double)result.Y - center) + (double)center);
result.Y = (int)(Math.Sin(rotationRadians) * ((double)result.X - (double)center) + (double)Math.Cos(rotationRadians) * ((double)result.Y - center) + (double)center);

But that's what I'm getting:

但这就是我得到的:

Result

结果

Any help would be much appreciated

任何帮助将非常感激

采纳答案by Fraser

The problem is int center = radiuswhich you are setting int radius = 576. This doesn't make sense as surely you are rotating about a point that should have an x and y location.

问题在于int center = radius您正在设置哪个int radius = 576。这没有意义,因为您肯定正在围绕应该具有 x 和 y 位置的点旋转。

Given you are rotating around the origin the center xand yshould both be 0not 576.

鉴于您正在围绕原点旋转中心,x并且y两者都应该0不是576

So, given that, try this.

所以,考虑到这一点,试试这个。

/// <summary>
/// Rotates one point around another
/// </summary>
/// <param name="pointToRotate">The point to rotate.</param>
/// <param name="centerPoint">The center point of rotation.</param>
/// <param name="angleInDegrees">The rotation angle in degrees.</param>
/// <returns>Rotated point</returns>
static Point RotatePoint(Point pointToRotate, Point centerPoint, double angleInDegrees)
{
    double angleInRadians = angleInDegrees * (Math.PI / 180);
    double cosTheta = Math.Cos(angleInRadians);
    double sinTheta = Math.Sin(angleInRadians);
    return new Point
    {
        X =
            (int)
            (cosTheta * (pointToRotate.X - centerPoint.X) -
            sinTheta * (pointToRotate.Y - centerPoint.Y) + centerPoint.X),
        Y =
            (int)
            (sinTheta * (pointToRotate.X - centerPoint.X) +
            cosTheta * (pointToRotate.Y - centerPoint.Y) + centerPoint.Y)
    };
}

Use like so.

像这样使用。

Point center = new Point(0, 0); 
Point newPoint = RotatePoint(blueA, center, 45);

Obviously if the center point is always 0,0then you can simplify the function accordingly, or else make the center point optional via a default parameter, or by overloading the method. You would also probably want to encapsulate some of the reusable math into other static methods too.

显然,如果中心点始终是,0,0那么您可以相应地简化函数,或者通过默认参数或重载方法使中心点成为可选。您可能还想将一些可重用的数学也封装到其他静态方法中。

e.g.

例如

/// <summary>
/// Converts an angle in decimal degress to radians.
/// </summary>
/// <param name="angleInDegrees">The angle in degrees to convert.</param>
/// <returns>Angle in radians</returns>
static double DegreesToRadians(double angleInDegrees)
{
   return angleInDegrees * (Math.PI / 180);
}

/// <summary>
/// Rotates a point around the origin
/// </summary>
/// <param name="pointToRotate">The point to rotate.</param>
/// <param name="angleInDegrees">The rotation angle in degrees.</param>
/// <returns>Rotated point</returns>
static Point RotatePoint(Point pointToRotate, double angleInDegrees)
{
   return RotatePoint(pointToRotate, new Point(0, 0), angleInDegrees);
}

Use like so.

像这样使用。

Point newPoint = RotatePoint(blueA, 45);

Finally, if you are using the GDI you can also simply do a RotateTransform. See: http://msdn.microsoft.com/en-us/library/a0z3f662.aspx

最后,如果您使用 GDI,您也可以简单地执行RotateTransform. 请参阅:http: //msdn.microsoft.com/en-us/library/a0z3f662.aspx

Graphics g = this.CreateGraphics();
g.TranslateTransform(blueA);
g.RotateTransform(45);

回答by Nick Bray

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

你的数学在我看来很奇怪。我认为 dx = r*Cos(theta) 和 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();