围绕另一个点旋转一个点

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

结果

现在我需要将这个点旋转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 ,你设置int radius = 576 。 这没有意义,因为你肯定在一个应该有x和y位置的点上旋转。

鉴于你在原点周围旋转,中心xy应该都是0而不是576

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

 ///  /// Rotates one point around another ///  /// The point to rotate. /// The center point of rotation. /// The rotation angle in degrees. /// Rotated point 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则可以相应地简化function,或者通过默认参数或通过重载方法使中心点可选。 您也可能希望将一些可重用的数学封装到其他静态方法中。

例如

 ///  /// Converts an angle in decimal degress to radians. ///  /// The angle in degrees to convert. /// Angle in radians static double DegreesToRadians(double angleInDegrees) { return angleInDegrees * (Math.PI / 180); } ///  /// Rotates a point around the origin ///  /// The point to rotate. /// The rotation angle in degrees. /// Rotated point 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); 

你的数学对我来说很奇怪。 我认为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();