如何确定哪个点最接近另一个点



我有两个列表,第一个列表包含矩形中心的x,y坐标,第二个列表包含连接矩形的直线端点的x,y,w,h坐标这些线的端点是x1=x,y1=y,x2=x+w,y2=y+h。我正试图找出线的哪个端点最靠近矩形的中心我知道我需要使用毕达哥拉斯定理来计算端点到矩形中心的距离,但我在正确确定哪些端点最接近正确矩形的中心时遇到了问题

互连矩形

示例

在该示例cx1中,cy1最接近的端点假定为x1,y1和cx2,cy2最接近的终点假定为(x2,y2(、(x3,y3(和(x5,y5(。其他矩形也是如此。蓝线表示哪个红点连接到哪个绿点。

distance = []
#masterStruc contains the x and y coordinates for the center of the rectangle
for z in range(len(masterStruc)):
cx = masterStruc[z][3]
cy = masterStruc[z][4]
#lineStruc contains the x, y, w, h coordinates for the end points of the line
for n in range(len(lineStruc)):
lx = lineStruc[n][0]
ly = lineStruc[n][1]
lw = lineStruc[n][2]
lh = lineStruc[n][3]
#Calculating the distances from the end points to the center of a rectangle
dist1 = int(math.sqrt((lx-cx)**2 + (ly-cy)**2))
dist2 = int(math.sqrt((lx+lw-cx)**2 + (ly+lh-cy)**2))
#Loop to compare which of the distances are closest to the center
for j in range(len(lineStruc)):
xx = lineStruc[n][0]
yy = lineStruc[n][1]
xw = lineStruc[n][2]
yh = lineStruc[n][3]
#Calculating the distances from the end points to the center of a rectangle
dist3 = int(math.sqrt((xx-cx)**2 + (yy-cy)**2))
dist4 = int(math.sqrt((xx+xw-cx)**2 + (yy+yh-cy)**2))
#Checking if the distance of the end points are closer
if(dist1 < dist4):
lx1 = lx
ly1 = ly
elif(dist1 < dist3):
lx1 = lx
ly1 = ly
elif(dist2 < dist4):
lx1 = lx + lw
ly1 = ly + lh
else:
lx1 = lx + lw
ly1 = ly + lh
#Storing the points of the closest end point and center of rectangle
distance.append([cx, cy, lx1, ly1])

由于有多个嵌套的for循环,您的代码很难阅读和理解。

一个好的做法是使用函数构造代码。你能在英语句子中描述的任何操作都应该是它自己的功能。尽可能避免嵌套循环;相反,用一个明确的名称将内部循环封装在一个函数中,该名称描述了该函数的作用。

请注意,实际上不需要使用math.sqrt,因为最小化距离等于最小化平方距离。

def squared_distance(p1, p2):
(x1, y1) = p1
(x2, y2) = p2
return (x2 - x1)**2 + (y2 - y1)**2
def get_closer_point(center, endpoints):
x, y, w, h = endpoints
p1 = (x, y)
p2 = (x + w, y + h)
if squared_distance(center, p1) <= squared_distance(center, p2):
return p1
else:
return p2
def get_closer_points(center_list, endpoints_list):
l = []
for c, e in zip(center_list, endpoints_list):
l.append(get_closer_point(c, e))
return l
# TESTING
centers =   [(0,0), (37, 42), (18, 12)]
endpoints = [(1,0,0,1), (30,40,5,3), (20,20,-5,-5)]
closer_points = get_closer_points(centers, endpoints)
print('centers:   ', centers)
print('endpoints: ', endpoints)
print('closer:    ', closer_points)
# centers:    [(0, 0), (37, 42), (18, 12)]
# endpoints:  [(1, 0, 0, 1), (30, 40, 5, 3), (20, 20, -5, -5)]
# closer:     [(1, 0), (35, 43), (15, 15)]

旁注:我们可以避免重新发明轮子,在python的标准库中寻找这个函数,而不是使用我们自己的函数来使用毕达哥拉斯定理计算两点之间的距离。事实证明,math.dist正是这样做的。因此,您可以导入math.dist,然后将上面代码中每次出现的squared_distance替换为math.dist。这很容易做到,因为我们已经将squared_distance封装为一个函数;如果我们每次需要毕达哥拉斯的公式时都明确地写出来,那就更麻烦了

最新更新