2013-07-20 136 views
0

我目前有一段代码可以围绕另一个点旋转一个点。问题是,当用户输入'90'时,我希望它是水平轴的旋转,其中当前90度指向下(垂直轴)。这里有一个图表,以澄清: enter image description here围绕另一个点旋转一个点 - 错误的方向

下面是当前的代码,我有:

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,反之亦然,以及试图捏造的数据方式,但没有运气。任何帮助将不胜感激!

+2

你就不能减去90从用户输入? –

+0

有两个问题:首先我测试了代码,并没有显示“当前”中描述的行为,即顺时针方向为正,但恰好相反,因此@ChrisSinclair的建议应该可以接受。第二个问题是:参考角度的确切点是什么? (0,90等)其唯一的用途是用于极坐标系(例如,该点位于距离X和Y度处),但函数的输出是笛卡尔坐标。因此唯一重要的是方向(+表示顺时针或逆时针)。 – varocarbas

+0

@ChrisSinclair不,不太合适;仅适用于我希望当前水平线指向下的情况。相反,从垂直线减去90度使其指向左侧。 – Ace

回答

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); 
+0

太棒了,谢谢你的回答!它真的清除了一切 – Ace

+0

不客气。 – varocarbas