22

特定のグラフィックを描くタスクがあります。このタスクの一部として、いくつかの点を 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);

しかし、それは私が得ているものです:

結果

どんな助けでも大歓迎です

4

2 に答える 2

55

問題は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);
于 2012-12-04T03:29:13.773 に答える
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();
于 2012-12-04T03:40:43.840 に答える