问题是int center = radius
你正在设置int radius = 576
。这没有任何意义,因为您肯定正在围绕应该具有 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)
};
}
像这样使用。
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);