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

C# 围绕另一个点旋转一个点,c#,rotation,gdi,trigonometry,C#,Rotation,Gdi,Trigonometry,我的任务是画一幅特定的图形。作为这项任务的一部分,我需要将一些圆点旋转45度 我已经花了两天的时间试图计算一个公式,但就是无法正确计算。 我已经搜索了所有的地方,包括这个特定的网站,我已经非常接近了,但我仍然不在那里 这是: 我需要画4个不同的点 我有一个特定的公式来计算位置,这超出了问题的范围,但我得到的结果如下: int radius = 576; int diameter = radius * 2; Point blueA = new Point(561, 273); Point gree

我的任务是画一幅特定的图形。作为这项任务的一部分,我需要将一些圆点旋转45度

我已经花了两天的时间试图计算一个公式,但就是无法正确计算。 我已经搜索了所有的地方,包括这个特定的网站,我已经非常接近了,但我仍然不在那里

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

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

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

现在我需要把这些点旋转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);
但这就是我得到的:


任何帮助都将不胜感激

问题在于您正在设置的
int-center=radius
。这毫无意义,因为你肯定是围绕一个应该有x和y位置的点旋转

如果您正在围绕原点旋转,则中心
x
y
都应该是
0
而不是
576

因此,考虑到这一点,试试这个

/// <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)
    };
}
显然,如果中心点始终为
0,0
,则可以相应地简化函数,或者通过默认参数或重载方法使中心点成为可选的。您可能还希望将一些可重用的数学封装到其他静态方法中

e、 g

最后,如果您使用的是GDI,您也可以简单地执行
旋转转换
。 见:


你的数学在我看来很奇怪。我认为dx=r*Cos(θ)和dy=r*Sin(θ)

这是我写的一个小程序,因为它困扰着我,我已经好几年没做过数学了

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

太完美了!非常感谢。这是当前的截图:你是对的。操作系统库的贡献者必须手动复制,因为括号被弄错了,在单元测试中出现错误。有人能指出一个资源来理解
RotatePoint
中的方程吗?@BryceGuinta这只是基本的触发器-我用这个公式来计算将某个东西旋转到固定位置所需的角度通过求解θ并将方程的左边设置为我想要的值来定位。对于
x=0
这个公式是
Math.Atan2(pointToRotate.Y-centerPoint.Y,centerPoint.x-pointToRotate.Y)
。请注意,常规atan不适用于所有象限,因此需要Atan2。我在代码的不同部分使用了该方法,在某个阶段,我得到了具有以下坐标的“上一个”点:X=576 Y=20。在这种情况下,我得到了“DivideByZeroException”,如果我将“try->catch”块放入其中,结果如下:
/// <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);
}
Point newPoint = RotatePoint(blueA, 45);
Graphics g = this.CreateGraphics();
g.TranslateTransform(blueA);
g.RotateTransform(45);
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();