2012-12-04 220 views
17

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

我已经花了2天试图计算一个公式,但只是无法正确理解。 我一直在搜索遍地,包括这个特定的网站,我越来越接近,但我仍然不在那里。

这就是: 我需要绘制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); 

但是,这就是我得到:

Result

任何帮助,将不胜感激

回答

35

的问题是int center = radius你是设置为int radius = 576。这是没有道理的,因为你肯定是在旋转一个应该有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); 
+0

我刚发布后就看到了这篇文章。这个公式起作用。 –

+0

它是完美的!谢谢。 以下是当前屏幕截图: http://s8.postimage.org/e7r44klcl/result.png –

+1

你说得对。 OS lib的贡献者必须手工复制它,因为括号被搞砸了,并且在单元测试中出错了。 –

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

我在代码的不同部分使用该方法,并且在某些阶段,我得到了带有这些坐标的“上一个”点:X = 576 Y = 20。 在这种情况下,我得到“DivideByZeroException” 这里是结果如果我把“try - > catch”块放在:http://s10.postimage.org/azjdc7rex/exception.png –