围绕另一个点旋转一个点

22

我有一个任务需要绘制一个特定的图形。作为这个任务的一部分,我需要将一些点旋转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);

result

现在我需要将这些圆点旋转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);

但这就是我得到的:

结果

非常感谢任何帮助。

3个回答

58

问题出在你设置了 int radius = 576,然后又设定了 int center = radius。这没意义,因为你应该围绕一个具有x和y位置的点旋转。

考虑到你是围绕原点旋转,因此中心点 xy 应该都是 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)
    };
}

使用方法如下。

Point center = new Point(0, 0); 
Point newPoint = RotatePoint(blueA, center, 45);
显然,如果中心点始终为0,0,则可以相应地简化函数,或者通过默认参数或方法重载使中心点变为可选项。您还可能希望将一些可重复使用的数学封装到其他静态方法中。

例如:

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

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

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

1
这是当前的屏幕截图: http://s8.postimage.org/e7r44klcl/result.png非常完美!谢谢。 - Allan Spreys
1
你是对的。一个操作系统库的贡献者必须手动复制,因为括号出了问题,在单元测试中出现了错误。 - C Bauer
有人可以指出一个资源来理解RotatePoint中的方程吗? - Bryce Guinta
1
@BryceGuinta 这只是基本的三角函数 - https://www.mathsisfun.com/algebra/trigonometry-index.html - Fraser
1
我使用了这个公式来计算旋转某物到固定位置所需的角度,通过解出θ并将方程左边设置为我想要的值。对于 x=0,这个公式是 Math.Atan2(pointToRotate.Y - centerPoint.Y, centerPoint.X - pointToRotate.Y)。请注意,常规的 atan 函数不适用于所有象限,因此需要使用 Atan2 函数。 - Bryce Guinta

1

你的数学看起来有点奇怪。我认为dx = r * Cos(theta),而dy = r * Sin(theta)。

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

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

我在代码的不同部分使用该方法,并在某个阶段使用这些坐标获取“previous”点:X = 576 Y = 20。在这种情况下,我会收到“DivideByZeroException”的错误提示。 如果我加入“try -> catch”块,结果如下所示:http://s10.postimage.org/azjdc7rex/exception.png - Allan Spreys

0
在一个类中旋转一个点。
public Koordinat DondurNoktaEtrafinda(Koordinat dondurmeMerkezi, double 
        dondurmeAcisiRadyan)
       {
       double cosAngle = Math.Cos(dondurmeAcisiRadyan); double sinAngle = 
 Math.Sin(dondurmeAcisiRadyan); 
      double newX = cosAngle * (X - dondurmeMerkezi.X) - sinAngle * (Y -dondurmeMerkezi.Y) + dondurmeMerkezi.X;
      double newY = sinAngle * (X - dondurmeMerkezi.X) + cosAngle * (Y -dondurmeMerkezi.Y) + dondurmeMerkezi.Y;
      double newZ = Z;

      return new Koordinat(newX, newY, newZ);
  }`

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接