def Find_Recent(x, y, x_, y_, k):
"""
找到点集中距离目标点(x, y)最近的k个点
:param x: 点集的x坐标
:param y: 点集的y坐标
:param x_: 目标点的x坐标
:param y_: 目标点的y坐标
:param k: 距离目标点最近的k个值
:return: k个与目标点最近的点的集合
"""
list_stack_temp = [] # 建立一个空的栈
for i in range(len(x)):
list_temp = []
if x[i] != x_:
length = math.sqrt(pow(x[i] - x_, 2) + pow(y[i] - y_, 2))
if len(list_stack_temp) < k:
list_stack_temp.append([(x[i], y[i]), length])
print("临时栈中多了一组数据,目前有" + str(len(list_stack_temp)) + "组数据")
else:
for m in list_stack_temp:
list_temp.append(m[1])
print("临时列表中有" + str(len(list_temp)) + "组数据")
list_temp.append(length)
list_temp.sort()
if length != list_temp[-1]:
last_ = list_temp[-1]
for n in list_stack_temp:
if n[1] == last_:
list_stack_temp.remove(n)
else:
continue
list_stack_temp.append([(x[i], y[i]), length])
else:
continue
else:
continue
return list_stack_temp
list1 = Find_Recent(x_discrete, y_discrete, x_discrete[43], y_discrete[43], 6)
x_ = []
y_ = []
for i in list1:
x_.append(i[0][0])
y_.append(i[0][1])
plt.scatter(x_discrete, y_discrete, c='y')
plt.scatter(x_discrete[43], y_discrete[43], c='b')
plt.scatter(x_, y_, c='r')
plt.show()
Python_寻找并绘制距离某一点最近的k个点
最新推荐文章于 2024-06-30 03:21:18 发布