在已知坐标的四个点附近找到一个点的最佳方法



我有四个点的坐标。有没有人能帮我找到一个点的坐标满足这个条件:从寻地到四个输入点的距离在1.9和2.5之间?

import numpy as np
dist_min = 1.9
dist_max = 2.5
# this show no points satisfied
input_points1 = [[ 7.57447956,  6.67658376, 10.79921475],
[ 8.98026868,  7.69010703, 12.89377068],
[ 6.22242062,  7.73362942, 12.87947421],
[ 10.0000000,  9.00000000, 8.500000000]]
#this has
input_points2 = [[ 7.57447956,  6.67658376, 10.79921475],
[ 8.98026868,  7.69010703, 12.89377068],
[ 6.22242062,  7.73362942, 12.87947421],
[ 6.22473072,  4.74175054, 12.96455411]]
def Distance(point1, point2):
return np.linalg.norm(point1 - point2)

找到一个随机点的方法:

import numpy as np

dist_min = 1.9
dist_max = 2.5
# this show no points satisfied
input_points1 = [[ 7.57447956,  6.67658376, 10.79921475],
[ 8.98026868,  7.69010703, 12.89377068],
[ 6.22242062,  7.73362942, 12.87947421],
[ 10.0000000,  9.00000000, 8.500000000]]
#this has
input_points2 = [[ 7.57447956,  6.67658376, 10.79921475],
[ 8.98026868,  7.69010703, 12.89377068],
[ 6.22242062,  7.73362942, 12.87947421],
[ 6.22473072,  4.74175054, 12.96455411]]
def Distance(point1, point2):
return np.linalg.norm(np.array(point1) - np.array(point2))

def find_point(input_points):
dmax = max([Distance(input_points[i], input_points[j]) 
for i in range(len(input_points)-1) 
for j in range(i+1, len(input_points))]) 
if dmax > 2 * dist_max:
return None
found = False
while not found:
ip = np.random.choice(len(input_points))
p = np.random.normal(size=3)
r = np.random.uniform(dist_min, dist_max)
x = p / np.linalg.norm(p) * r + np.array(input_points[ip])
found = True
for i in input_points:
d = Distance(i, x)
if d <= dist_min or d >= dist_max:
found = False
continue
return(x)
a = find_point(input_points1)
print(a)  
# NONE
b = find_point(input_points2)
print([Distance(i, b) for i in input_points2])
# [2.4877643881304805, 2.1439232926982417, 2.2860134633791795, 1.9466840567560841]

这看起来像是你可以使用K-Means(链接到维基百科)函数,只有1个质心,然后检查点的距离是否与数据中所有点的距离正确。也许这不是最优雅或最有效的解决方案,但它应该可以工作。

K-Means代码改编自本教程的K-Means:

import pandas as pd
from sklearn.cluster import KMeans
# data is whatever set of points you have
df = pd.DataFrame(data)
# fit k means with 1 centroid lol
k_means = KMeans(n_clusters=1)
labels=k_means.fit_predict(df)
centroid = k_means.cluster_centers_[:,0]
# compare to all points
for point in data:
assert distance(point, centroid) >= 1.9
assert distance(point, centroid) <= 2.5

最新更新