简体   繁体   English

如何找到与另一个点最近点的边上的点

[英]How to find the point on an edge which is the closest point to another point

I'm searching the way to efficiently find the point on an edge which is the closest point to some other point. 我正在寻找有效地找到边缘上的点的方法,该点是与其他点最接近的点。

Let's say I know two points which are vertices of the edge. 假设我知道两个点是边的顶点。 I can calculate the equation of the line that crosses those points. 我可以计算穿过这些点的线的方程。

What is the best way to calculate the point on the edge which is the closest point to some other point in the plane. 计算边缘上的点的最佳方法是什么,该点是平面中某个其他点的最近点。

I would post an image but I don't have enough reputation points. 我会发布一张图片,但我没有足够的声望点。

Let's assume the line is defined by the two points (x1,y1), (x2,y2) and the “other point” is (a,b). 假设该线由两个点(x1,y1),(x2,y2)定义,而“另一个点”是(a,b)。 The point you're looking for is (x,y). 你要找的点是(x,y)。

在此输入图像描述

You can easily find the equation of the black line. 您可以轻松找到黑线的等式。 To find the blue line equation use the fact that m1*m2=-1 (m1 and m2 are the slopes of the two lines). 要找到蓝线方程,请使用m1 * m2 = -1(m1和m2是两条线的斜率)的事实。

Clearly, the point you're looking for is the intersection between the two lines. 显然,您要寻找的是两条线之间的交叉点。

在此输入图像描述

There are two exceptions to what I was saying: 我所说的有两个例外:

  1. If x1=x2 then (x,y)=(x1,b). 如果x1 = x2则(x,y)=(x1,b)。
  2. If y1=y2 then (x,y)=(a,y1). 如果y1 = y2则(x,y)=(a,y1)。

The following Python function finds the point (if you don't know Python just think of it as a psudo-code): 以下Python函数找到了重点(如果您不知道Python只是将其视为psudo代码):

def get_closest_point( x1,y1, x2,y2, a,b ):
    if x1==x2: return (x1,b)
    if y1==y2: return (a,y1)
    m1 = (y2-y1)/(x2-x1)
    m2 = -1/m1
    x = (m1*x1-m2*a+b-y1) / (m1-m2)
    y = m2*(x-a)+b
    return (x,y)

You have three zones to consider. 您需要考虑三个区域。 The "perpendicular" approach is for the zone in the middle: “垂直”方法适用于中间区域:

在此输入图像描述

For the other two zones the distance is the distance to the nearest segment endpoint. 对于其他两个区域,距离是到最近的区段端点的距离。

The equation for the segment is: 该细分的等式是:

y[x] = m x + b

Where 哪里

  m -> -((Ay - By)/(-Ax + By)), 
  b -> -((-Ax By + Ay By)/(Ax - By))  

And the perpendiculars have slope -1/m 并且垂线的斜率为-1 / m

The equations for the perpendicular passing thru A is: 垂直通过A的方程是:

  y[x] = (-Ax + By)/(Ay - By) x + (Ax^2 + Ay^2 - Ax By - Ay By)/(Ay - By)

And the perpendicular passing thru B is the same exchanging the A's and B's in the equation above. 通过B的垂直通过在上面的等式中交换A和B是相同的。

So you can know in which region lies your point introducing its x coordinate in the above equations and then comparing the y coordinate of the point with the result of y[x] 所以你可以知道你的点在哪个区域引入上述方程中的x坐标,然后将点的y坐标与y [x]的结果进行比较

Edit 编辑

How to find in which region lies your point? 如何找到您的观点在哪个区域?

Let's suppose Ax ≤ Bx (if it's the other way, just change the point labels in the following formulae) 我们假设Ax≤Bx(如果是另一种方式,只需更改以下公式中的点标签)

We will call your point {x0,y0} 我们会给你的观点{x0,y0}

1) Calculate 1)计算

 f[x0] =  (-Ax + By)/(Ay - By) x0 + (Ax^2 + Ay^2 - Ax By - Ay By)/(Ay - By)

and compare with y0. 并与y0进行比较。

If y0 > f[x0], then your point lies in the green field in the figure above and the nearest point is A. 如果y0> f [x0],那么你的点位于上图中的绿色区域,最近的点是A.

2) Else, Calculate 2)否则,计算

g[x0] =  (-Bx + Ay)/(By - Ay) x0 + (Bx^2 + By^2 - Bx Ay - By Ay)/(By - Ay)  

and compare with y0. 并与y0进行比较。

If y0 < g[x0], then your point lies in the yellow field in the figure above and the nearest point is B. 如果y0 <g [x0],那么你的点位于上图中的黄色区域,最近的点是B.

3) Else, you are in the "perpendicular light blue zone", and any of the other answer tell you how to calculate the nearest point and distance (I am not going to plagiarize :)) 3)否则,你处于“垂直浅蓝色区域”,任何其他答案告诉你如何计算最近的点和距离(我不会剽窃:))

HTH! HTH!

I can describe what you want to do in geometric terms, but I don't have the algorithm at hand. 我可以用几何术语描述你想要做什么,但我手头没有算法。 Will that help? 这会有帮助吗?

Anyway, you want to draw a line which contains the stray point and is perpendicular to the edge. 无论如何,您想绘制一条包含杂散点并垂直于边缘的直线。 I think the slopes are a negative inverse relation between perpendicular lines, if that helps. 我认为斜率是垂直线之间的负反比关系,如果这有帮助的话。

Then you want to find the intersection of the two lines. 然后你想找到两条线的交点。

Let's stick with the 2D case to save typing. 让我们坚持使用2D案例来节省打字。 It's been a while, so please forgive any elementary mistakes in my algebra. 已经有一段时间了,所以请原谅我的代数中的任何基本错误。

The line forming the edge between the two points (x1, y1), (x2, y2) is represented as a function 形成两点(x1,y1),(x2,y2)之间的边缘的线表示为函数

y = mx + b

(You get to figure out m and b yourself, but it's elementary) (你可以自己弄清楚m和b,但这是基本的)

What you want to do is minimize the distance from your point (p1, p2) to a point on this line, ie 你想要做的是最小化你的点(p1,p2)到这一点上的点的距离,即

(p1-x)^2 + (p2-y)^2     (equation I)

subject to the equation 受到等式的影响

y = mx + b              (equation II)

Substitute equation II into equation I and solve for x. 将等式II代入等式I并求解x。 You'll get two solutions; 你会得到两个解决方案; pick the one which gives the smaller value in equation I. 选择在等式I中给出较小值的那个。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM