我想知道如何从水平轴X获取线段A-B的角度。SO上的其他问题只涉及两条直线之间的角度。我知道我可以始终画第二条线段A-C并进行计算,但我想知道是否有更快的方法。
编辑:我非常确定我没有做过早的优化。
atan
来实现此功能。angle = atan((By-Ay)/(Bx-Ax))
private double Angulo(int x1, int y1, int x2, int y2)
{
double degrees;
// Avoid divide by zero run values.
if (x2 - x1 == 0)
{
if (y2 > y1)
degrees = 90;
else
degrees = 270;
}
else
{
// Calculate angle from offset.
double riseoverrun = (double)(y2 - y1) / (double)(x2 - x1);
double radians = Math.Atan(riseoverrun);
degrees = radians * ((double)180 / Math.PI);
// Handle quadrant specific transformations.
if ((x2 - x1) < 0 || (y2 - y1) < 0)
degrees += 180;
if ((x2 - x1) > 0 && (y2 - y1) < 0)
degrees -= 180;
if (degrees < 0)
degrees += 360;
}
return degrees;
}
public static int GetAngleBetweenPoints(PointF pt1, PointF pt2)
{
float dx = pt2.X - pt1.X;
float dy = pt2.Y - pt1.Y;
int deg = Convert.ToInt32(Math.Atan2(dy, dx) * (180 / Math.PI));
if (deg < 0) { deg += 360; }
return deg;
}
如果您的线条是以[r_x,r_y]
的形式表示,其中r_x
表示x方向的变化,r_y
表示y方向的变化,您也可以使用arccosine函数。
angle = arccos( r_x/( r_x*r_x + r_y*r_y ) )
这个说法有点晦涩,但基本上是点积定理:
angle = arccos (r . v)
r
和v
都是单位向量(长度为1的向量)。在我们的情况下,v
是向量[1,0]
,而r
是
[r_x,r_y] / (r_x^2+r_y^2)
如果
那么就有一个快速的解决方案:在这些条件下,你可以假设 tan(a) = a = atan(a),因此可以省略掉 atan() 方法的调用。
横轴实际上是一条方程为
y = 0
的直线,因此您可以使用已有的解决方案。
sine
或cosine
。如果在这种情况下是真的,并且你关心性能,那么你可能不应该实际计算角度。但是只有根据你打算用角度做什么才能做出建议。 - sigfpe