如何判断一个点是否在某条线附近?
我问“我如何判断是否一个点属于某条线?”之前,我找到了一个合适的答案,非常感谢。
现在,我想知道如何判断某个点是否接近我的线。
I asked "How can I tell if a point belongs to a certain line?" before and I found a suitable answer so thank you very much.
Now, I would like to know how to tell if a certain point is close to my line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
@Alan Jackson 的答案几乎是完美的 - 但他的第一个(也是投票最多的)评论表明端点没有得到正确处理。 为了确保该点位于线段上,只需创建一个框,其中该线段是对角线,然后检查该点是否包含在其中。 下面是伪代码:
给定线 ab,由点 a 和 b 以及点 p 组成,如下所示:
@Alan Jackson's answer is almost perfect - but his first (and most up-voted) comment suggests that endpoints are not correctly handled. To ensure the point is on the segment, simply create a box where the segment is a diagonal, then check if the point is contained within. Here is the pseudo-code:
Given Line ab, comprised of points a and b, and Point p, in question:
您需要计算到直线的直角距离。 然后您必须定义什么是“接近”并测试它是否在该距离内。
您想要的方程是:
You need to calculate the right angle distance to the line. Then you have to define what "close" is and test if it is within that distance.
The equation you want is:
基本上,您想要做的就是找到与您的点和线相交的法线(即垂直于您的线的线),然后计算沿该线的距离。
Basically, what you want to do it find the normal line — that is, a line perpendicular to your line — that intersects your point and the line, and then compute the distance along that line.
这是一个可以解决这个问题的 python 函数。 它应该在 2 或 3 维(或更多)维中工作,并处理垂直和水平线,没有特殊情况。 如果将
clipToSegment
设置为 true,则当投影线延伸超出提供的线段时,返回的点将被剪裁到末端。用法:(点[4,5]距[2,4]到[4,6]线段的距离)
Here's a python function which does the trick. It should work in 2 or 3 dimensions (or more) and handles vertical and horizontal lines without special cases. If you set
clipToSegment
to true the returned point is clipped to the ends if the projected line extends beyond the supplied line segment.Usage: (distance of point [4,5] from the line segment from [2,4] to [4,6])
近到什么程度呢?
一些几何学会给你你需要的答案,你只需要注意以下步骤。
假设您的喜欢的形式为 y=mx+b,到您的点的最短距离将是垂直于您的起始线 (m1=-1/m) 的线,与您所讨论的点相交。
从那里您可以计算交点和相关点之间的距离。
How close is near?
Some geometry will give you the answer you need, you just need to be aware of the following steps.
Assuming your like is of the form y=mx+b, the shortest distance to your point will be the line perpendicular to your starting line (m1=-1/m), intersecting your point in question.
From there you calculate the distance between the intersection point and the point in question.
计算线上最接近该点的点。
假设线段为a、b,点为p。
这给你“量”,即你在 A 和 B 之间的线段有多远(正确的边界)。
给你点(nx,ny)。
这将正确地超出线段的末尾,因为它将“金额”保持在 0 和 1 之间。
如果您不希望它是有界线段,请摆脱金额的界限。 代码的其余部分仍然可以工作,计算 A 之外和之前以及 B 之外的位置。
还有另一个问题声称这个问题是重复的,但是,它要求不同的东西,因此我的解决方案解决了点的位置,然后只是解决了欧几里德距离(实际上解决了这两个问题)。
a.distanceSq(b) 也可以作为 vABxvABx + vAByvABy 完成,因为我们已经完成了这些。
Calculate the point on your line that is closest to that point.
Assuming the line segment is a and b, and the point is p.
Which gives you 'amount', how far through the line segment you are between A and B (properly bounded).
Gives you point (nx,ny).
This will properly work beyond the end of the line segment, because it keeps 'amount' between 0 and 1.
If you don't want it a bounded line segment get rid of the bounds for amount. The rest of the code will still work, calculating positions beyond and before A and beyond B.
There was another question that claimed this question was a duplicate but, it's asking for a different thing hence my solution solves for the position of the point and then just solves the Euclidean distance (which actually solves both questions).
a.distanceSq(b) can also be done as vABxvABx + vAByvABy, since we already have those done.
Google 是您的朋友:点线距离(二维)。 您只需使用底部的方程式即可。
Google is your friend: Point-Line Distance (2-Dimensional). You can just use the equation at the bottom and there you go.