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

正如评论所说,包含函数中的计算很好:旋转点按预期计算(负角表示顺时针旋转)。您所追求的是基于极坐标系的旋转点的定义,这必须在稍后阶段完成。“Desired”中描述的参考系统与“默认参考系统”有 +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 回答