C# 围绕另一点旋转一点-方向错误
我现在有一段代码,可以围绕另一个点旋转一个点。问题是,当用户输入'90'时,我希望它是水平轴的旋转,当前90度指向下(垂直轴)。下面是一张示意图来说明: 这是我目前掌握的代码:C# 围绕另一点旋转一点-方向错误,c#,wpf,drawing,C#,Wpf,Drawing,我现在有一段代码,可以围绕另一个点旋转一个点。问题是,当用户输入'90'时,我希望它是水平轴的旋转,当前90度指向下(垂直轴)。下面是一张示意图来说明: 这是我目前掌握的代码: public static Point RotatePoint(Point pointToRotate, Point centerPoint, int angleInDegrees) { double angleInRadians = angleInDegrees * (Math.PI / 18
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,反之亦然,也尝试过在输入的过程中伪造数据,但没有成功。任何帮助都将不胜感激 如通过注释所述,包含函数中的计算很好:旋转点按预期计算(负角度表示顺时针旋转)。您所追求的是基于极坐标系的旋转点的定义,这必须在稍后阶段完成。“所需”中描述的参考系相对于“默认参考系”有+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;
}
你不能从用户输入中减去90吗?有两个问题:首先,我已经测试了代码,它没有显示“Current”上描述的行为,即顺时针方向为正,但正好相反,因此@ChrisSinclair的建议应该可以接受。第二个问题是:参考角的确切点是什么?(0、90等)其唯一实用程序用于极坐标系(例如,点位于距离X和Y度处),但函数的输出为笛卡尔坐标。因此,唯一真正重要的是方向(+表示顺时针或逆时针-)@ChrisSinclair-Nope,不太管用;仅适用于希望当前水平线指向下方的情况。从垂直线减去90度,使其指向左侧。@varocarbas我使用该函数围绕“起点”旋转和“终点”,然后从“起点->终点”绘制一条线。目前的实施是按常规轮流进行的。虽然当渲染时,它显示为CW,因为它被绘制到画布上:(0,0)是左上角,(最大X,0)是右上角,(0,最大Y)是左下角,我认为你需要澄清一下你的想法;请看一下我的答案。
Point rotatedPoint = RotatePoint(curPointnew, centerPoint, rotationAngle);
double angleRotatedPoint = angleFromPoint(rotatedPoint, centerPoint);