如何在 C、C# / .NET 2.0 或 Java 中计算所有情况下点和线段之间的最短 2D 距离?
可能的重复:
点和线段之间的最短距离 < /p>
i我正在寻找一种方法来计算所有情况下的最小距离。我发现的解决方案的问题是:
图形概念图的解决方案显示点始终与线段垂直,因此它位于“线段端点之间”。我的几何技能很糟糕,所以我无法验证这些解决方案是否适用于所有情况。
算法解决方案是:使用 Fortran 或其他一些我不完全理解的语言, b:被人们标记为不完整,c:调用未以任何方式描述的方法/函数(被认为是微不足道的)。
2 a、b 和 c 的一个很好的例子是
我将二维线段作为双精度类型坐标对(x1,y1),(x2,y2),将点作为双精度类型坐标(x3,y3)。 C#/Java/C 解决方案都受到赞赏。
感谢您的回答& BR:马蒂
Possible Duplicate:
Shortest distance between a point and a line segment
i am looking for a way to calculate the minimum distance in all cases. the problems with solutions i found are:
Solutions with graphical conceptual drawings show point always on perpendicular from line segment so it's "between line segment's end points". My geometry skills are horrible so i can't verify that these solutions work in all cases.
Algorithm solutions are a: with fortran or some other language i don't fully understand,
b: are flagged as incomplete by people, c: calling methods/functions that are not described in any way (considered trivial).
Good example of 2 a, b and c is
Shortest distance between a point and a line segment
i have the 2D line segment as double-type co-ordinate pair (x1, y1), (x2,y2) and point as double type co-ordinate (x3,y3). C#/Java/C solutions are all appreciated.
Thanks for your answers & BR: Matti
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
还回答了点和线段之间的最短距离 因为它收集了所有语言的解决方案。答案也放在这里,因为这个问题专门询问 C# 解决方案。这是从 http://www.topcoder.com/ 修改的tc?d1=tutorials&d2=geometry1&module=Static :
Answered also Shortest distance between a point and a line segment because that gathers solutions in all languages. Answer put also here because this questions asks specifically a C# solution. This is modified from http://www.topcoder.com/tc?d1=tutorials&d2=geometry1&module=Static :
如果您有线
L:
A * x + B * y + C = 0
则从该线到点
(x1, y1)
的距离为abs(A * x1 + B * y1 + C) / sqrt(A * A + B * B)
。在你的情况下,如果你有间隔,(xa, ya); (xb, yb)
你应该找到min( distance(x1, y1, xa, ya), distance(x1, y1, xb, yb))
然后看看是否与(x1,y1)
到线L在区间上,那么答案就是距离就是它。否则两个距离的最小值。If you have line
L:
A * x + B * y + C = 0
Then distance from this line to point
(x1, y1)
isabs(A * x1 + B * y1 + C) / sqrt(A * A + B * B)
. in your case if you has interval,(xa, ya); (xb, yb)
you should findmin( distance(x1, y1, xa, ya), distance(x1, y1, xb, yb))
then see if perpendecular from(x1, y1)
to line L is on the interval, then the answer is the distance is it. otherwise min of two distances.