0

私は現在、ある点を別の点について回転させる作業コードを持っています。問題は、ユーザーが「90」と入力したときに、現在90度が下を向いている水平軸(垂直軸)への回転にしたいことです。明確にするための図を次に示します。 ここに画像の説明を入力

そして、これが私が持っている現在のコードです:

    public static Point RotatePoint(Point pointToRotate, Point centerPoint, int angleInDegrees) {
        double angleInRadians = angleInDegrees * (Math.PI / 180);
        double cosTheta = Math.Cos(angleInRadians);
        double sinTheta = Math.Sin(angleInRadians);
        int x = (int)
                (cosTheta * (pointToRotate.X - centerPoint.X) -
                sinTheta * (pointToRotate.Y - centerPoint.Y) + centerPoint.X);
        int y = (int)
                (sinTheta * (pointToRotate.X - centerPoint.X) +
                cosTheta * (pointToRotate.Y - centerPoint.Y) + centerPoint.Y);
    }

y=x を作成しようとしたり、その逆を行ったり、途中でデータをごまかそうとしたりしましたが、うまくいきませんでした。どんな助けでも大歓迎です!

4

1 に答える 1

1

コメントで述べたように、含まれている関数の計算は問題ありません。回転したポイントは期待どおりに計算されます (負の角度は時計回りの回転を意味します)。あなたが求めているのは、極座標系に基づく回転点の定義であり、これは後の段階で行う必要があります。「望ましい」に描かれている基準系は、「既定の基準系」に対して +90 度のギャップがあるため、そのようなシステムに対して実行される計算に +90 を追加するだけで済みます。

あなたが求めている角度は、次の関数に依存して計算できます。

public static double angleFromPoint(Point inputPoint, Point centerPoint)
{
    double varX1 = Math.Abs(inputPoint.X - centerPoint.X);
    double varY1 = Math.Abs(inputPoint.Y - centerPoint.Y);

    double outAngle = 180 * Math.Atan(varY1 / varX1) / Math.PI; //Angle from 0 to 90 which has to be updated on account of the quadrant it is in and the chosen syst

    int curQuadrant = determineQuadrant(inputPoint, centerPoint);

    //Modifications to account for the default system of reference
    if (curQuadrant == 1)
    {
        outAngle = 180 - outAngle;
    }
    else if (curQuadrant == 3)
    {
        outAngle = 360 - outAngle;
    }
    else if (curQuadrant == 4)
    {
        outAngle = 180 + outAngle;
    }

    //Over-modification to account for the system of reference "Desired", +90 the default system of reference
    outAngle = outAngle + 90;


    if (outAngle > 360)
    {
        outAngle = outAngle - 360;
    }

    return outAngle;
}

//Moving clockwisely, the first quadrant is located between 180 and 90 degrees in the default system of reference 
public static int determineQuadrant(Point inputPoint, Point centerPoint)
{
    int curQuadrant = 0;

    if (inputPoint.X < centerPoint.X && inputPoint.Y >= centerPoint.Y)
    {
        //Default system of reference -> 180 to 90
        curQuadrant = 1;
    }
    else if (inputPoint.X >= centerPoint.X && inputPoint.Y >= centerPoint.Y)
    {
        //Default system of reference -> 90 to 0/360
        curQuadrant = 2;
    }
    else if (inputPoint.X >= centerPoint.X && inputPoint.Y < centerPoint.Y)
    {
        //Default system of reference -> 0/360 to 270
        curQuadrant = 3;
    }
    else if (inputPoint.X < centerPoint.X && inputPoint.Y < centerPoint.Y)
    {
        //Default system of reference -> 270 to 180
        curQuadrant = 4;
    }

    return curQuadrant;
}

そこでは、「デフォルトの参照システム」に基づいた段階的で明確な計算と、その後の必要なものへの変換を確認できます。計算は ArcTangent (0 ~ 90 の角度のみを提供) に基づいており、指定された「象限」 (既定のシステム、つまり目的のシステムの -90 に基づく) に基づいて更新されます。計算された角度 + 90 により、必要な結果が得られます。

したがって、最初に回転したポイントの座標を計算し、次に関連する角度を計算する必要があります。

Point rotatedPoint = RotatePoint(curPointnew, centerPoint, rotationAngle);
double angleRotatedPoint = angleFromPoint(rotatedPoint, centerPoint);
于 2013-07-22T08:20:39.300 に答える