問題は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);