简体   繁体   English

如何判断一个点是否在某条线附近?

[英]How can I tell if a point is nearby a certain line?

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. 现在,我想知道如何确定某一点是否接近我的界限。

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: 你想要的等式是:

d = | V ^^·R | =(|(X_2-X_1)(Y_1-y_0) - (X_1-X_0)(Y_2-Y_1)|)/(SQRT((X_2-X_1)^ 2 +(y_2- Y_1)^ 2))。

@Alan Jackson 's answer is almost perfect - but his first (and most up-voted) comment suggests that endpoints are not correctly handled. @Alan Jackson的答案几乎是完美的 - 但他的第一个(也是最多投票的)评论表明端点没有得到正确处理。 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: 给定线ab,包括点a和b,以及点p,有问题:

int buffer = 25;//this is the distance that you would still consider the point nearby
Point topLeft = new Point(minimum(a.x, b.x), minimum(a.y, b.y));
Point bottomRight = new Point(maximum(a.x, b.x), maximum(a.y, b.y));
Rect box = new Rect(topLeft.x - buffer, topLeft.y - buffer, bottomRight.x + buffer, bottomRight.y + buffer);
if (box.contains(p))
{
    //now run the test provided by Alan
    if (test)
        return true;
}
return false;

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. 假设线段是a和b,并且该点是p。

float vAPx = p.x - a.x;
float vAPy = p.y - a.y;
float vABx = b.x - a.x;
float vABy = b.y - a.y;
float sqDistanceAB = a.distanceSq(b);
float ABAPproduct = vABx*vAPx + vABy*vAPy;
float amount = ABAPproduct / sqDistanceAB;
if (amount > 1) amount = 1;
if (amount < 0) amount = 0;

Which gives you 'amount', how far through the line segment you are between A and B (properly bounded). 这给你'数量',你在A和B之间的线段有多远(正确有界)。

    float nx = (amount * (b.x - a.x)) + a.x;
    float ny = (amount * (b.y - a.y)) + a.y;

Gives you point (nx,ny). 给你点(nx,ny)。

if (p.distance(nx,ny) > threshold) reject;

This will properly work beyond the end of the line segment, because it keeps 'amount' between 0 and 1. 这将在线段末尾之后正常工作,因为它将“数量”保持在0和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. 其余的代码仍然可以工作,计算A和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 vABx vABx + vABy vABy, since we already have those done. a.distanceSq(b)也可以作为vABx vABx + vABy vABy来完成,因为我们已经完成了这些操作。

Here's a python function which does the trick. 这是一个python函数,可以解决这个问题。 It should work in 2 or 3 dimensions (or more) and handles vertical and horizontal lines without special cases. 它应该工作在2维或3维(或更多),并处理垂直和水平线,没有特殊情况。 If you set clipToSegment to true the returned point is clipped to the ends if the projected line extends beyond the supplied line segment. 如果将clipToSegment设置为true,则如果投影线超出提供的线段,则返回的点将被剪切到末尾。

def nearestPointOnLine(pt, r0, r1, clipToSegment = True):
    r01 = r1 - r0           # vector from r0 to r1 
    d = np.linalg.norm(r01) # length of r01
    r01u = r01 / d          # unit vector from r0 to r1
    r = pt - r0             # vector from r0 to pt
    rid = np.dot(r, r01u)   # projection (length) of r onto r01u
    ri = r01u * rid         # projection vector
    lpt = r0 + ri           # point on line

    if clipToSegment:       # if projection is not on line segment
        if rid > d:         # clip to endpoints if clipToSegment set
            return r1
        if rid < 0:
            return r0 

    return lpt

Usage: (distance of point [4,5] from the line segment from [2,4] to [4,6]) 用法:(点[4,5]距离线段[2,4]到[4,6])

r0 = np.array([2,4])
r1 = np.array([4,6])
rpt = np.array([4,5])
pt = nearestPointOnLine(rpt, r0, r1, True)

dist = np.linalg.norm(rpt-pt)
print('dist', dist)

Google is your friend: Point-Line Distance (2-Dimensional) . 谷歌是你的朋友: 点线距离(二维) You can just use the equation at the bottom and there you go. 您可以在底部使用等式,然后就可以了。

基本上,你想要做的就是找到法线 - 也就是垂直于你的线的线 - 与你的点和线相交,然后计算沿着那条线的距离。

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. 假设你的形状是y = mx + b,那么到你的点的最短距离将是垂直于你的起始线的线(m1 = -1 / m),与你所讨论的点相交。

From there you calculate the distance between the intersection point and the point in question. 从那里计算交叉点和相关点之间的距离。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何判断一个点是否属于某一行? - How can I tell if a point belongs to a certain line? 我怎么知道什么时候/什么加载某些程序集? - How can I tell when/what is loading certain Assemblies? 选择错误答案后如何告诉我的程序 go 回到某个点? - How do I tell my program to go back to a certain point after the wrong answer is selected? 我可以告诉NHibernate不要保存某些对象吗? - Can I tell NHibernate not to save certain objects? 如何判断一个点是在线的右侧还是左侧 - How to tell whether a point is to the right or left side of a line 如何判断Unicode代码点是否是一个完整的可打印字形(或字形簇)? - How can I tell if a Unicode code point is one complete printable glyph(or grapheme cluster)? 如何显示只有1个数据点的折线图 - How can I display a line chart with only 1 data point 如何告诉 C# 中的 DateTime 将日期设置为某种语言? - How can I tell DateTime in C# to set the date to a certain language? 如何在没有物理/光线投射的情况下检测附近的游戏对象? - How can I detect nearby GameObjects without physics/raycast? 如何告诉Swagger不要在输入类中将某些字段显示为参数? - How can I tell Swagger to not show certain fields in my input class as parameters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM