计算两条线之间的角度而不需要计算斜率? (爪哇)
我有两条线:L1 和 L2。我想计算两条线之间的角度。 L1 有点:{(x1, y1), (x2, y2)}
,L2 有点:{(x3, y3), (x4, y4)}
。
如何计算这两条线之间形成的角度,而无需计算斜率?我当前遇到的问题是,有时我有水平线(沿 x 轴的线),并且以下公式失败(除以零例外):
arctan((m1 - m2) / (1 - (m1 * m2)))
其中 m1
和 m2
分别是线 1 和线 2 的斜率。是否有一个公式/算法可以计算两条线之间的角度而不会出现被零除的异常?任何帮助将不胜感激。
这是我的代码片段:
// Calculates the angle formed between two lines
public static double angleBetween2Lines(Line2D line1, Line2D line2)
{
double slope1 = line1.getY1() - line1.getY2() / line1.getX1() - line1.getX2();
double slope2 = line2.getY1() - line2.getY2() / line2.getX1() - line2.getX2();
double angle = Math.atan((slope1 - slope2) / (1 - (slope1 * slope2)));
return angle;
}
谢谢。
I have two Lines: L1 and L2. I want to calculate the angle between the two lines. L1 has points: {(x1, y1), (x2, y2)}
and L2 has points: {(x3, y3), (x4, y4)}
.
How can I calculate the angle formed between these two lines, without having to calculate the slopes? The problem I am currently having is that sometimes I have horizontal lines (lines along the x-axis) and the following formula fails (divide by zero exception):
arctan((m1 - m2) / (1 - (m1 * m2)))
where m1
and m2
are the slopes of line 1 and line 2 respectively. Is there a formula/algorithm that can calculate the angles between the two lines without ever getting divide-by-zero exceptions? Any help would be highly appreciated.
This is my code snippet:
// Calculates the angle formed between two lines
public static double angleBetween2Lines(Line2D line1, Line2D line2)
{
double slope1 = line1.getY1() - line1.getY2() / line1.getX1() - line1.getX2();
double slope2 = line2.getY1() - line2.getY2() / line2.getX1() - line2.getX2();
double angle = Math.atan((slope1 - slope2) / (1 - (slope1 * slope2)));
return angle;
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
atan2
函数减轻了处理atan
的痛苦。它被声明为
double atan2(double y, double x)
并将直角坐标(x,y)
从极坐标转换为角度theta
坐标(r,theta)
所以我将你的代码重写为
The
atan2
function eases the pain of dealing withatan
.It is declared as
double atan2(double y, double x)
and converts rectangular coordinates(x,y)
to the angletheta
from the polar coordinates(r,theta)
So I'd rewrite your code as
在这种情况下,点积可能更有用。 在这里您可以找到一个 Java 几何包,它提供了一些有用的帮助器。下面是他们用于确定两个 3-d 点之间角度的计算。希望它能让您开始:
祝您好运!
Dot product is probably more useful in this case. Here you can find a geometry package for Java which provides some useful helpers. Below is their calculation for determining the angle between two 3-d points. Hopefully it will get you started:
Good luck!
两个向量的点积等于角度的余弦乘以两个向量的长度。这将计算点积,除以向量的长度,并使用反余弦函数来恢复角度。
The dot product of 2 vectors is equal to the cosine of the angle time the length of both vectors. This computes the dot product, divides by the length of the vectors and uses the inverse cosine function to recover the angle.
也许我的 Android 坐标系统方法对某人有用(使用 Android PointF 类来存储点)
它为任何象限返回以度为单位的正值:0 <= x < 360
你可以在这里查看我的实用类
Maybe my approach for Android coordinates system will be useful for someone (used Android PointF class to store points)
It return positive value in degrees for any quadrant: 0 <= x < 360
You can checkout my utility class here
获取角度的公式为
tan a = (slope1-slope2)/(1+slope1*slope2)
您正在使用:
所以它应该是:
The formula for getting the angle is
tan a = (slope1-slope2)/(1+slope1*slope2)
You are using:
So it should be:
首先,你确定括号的顺序正确吗?我认为(可能是错误的)应该是这样的:
其次,您可以对除零的 div 做两件事:您可以捕获异常并处理它
……或者您可以检查您的除数是否不为零 在尝试操作之前。
First, are you sure the brackets are in the right order? I think (could be wrong) it should be this:
Second, there are two things you could do for the div by zero: you could catch the exception and handle it
...or you could check that your divisors are never zero before you attempt the operation.
检查这个Python代码:
Check this Python code: