这段代码需要很长的时间来找到一个点。有没有更好的方法来找到一个点,是等距的4个给定的点?公差为0.5是罚款。
import numpy as np
# Define the four 3D points as (x, y, z) coordinates
point1 = np.array([1, 2, 3])
point2 = np.array([3, 4, 5])
point3 = np.array([5, 6, 7])
point4 = np.array([7, 8, 9])
# Set a tolerance for distance equality
tolerance = 0.5
# Calculate the initial average point
average_point = np.mean([point1, point2, point3, point4], axis=0)
while True:
distances = np.linalg.norm(average_point - np.array([point1, point2, point3, point4]), axis=1)
max_distance = np.max(distances)
min_distance = np.min(distances)
if max_distance - min_distance <= tolerance:
break
# Update the average point by moving it closer to the farthest point
farthest_point = np.argmax(distances)
average_point = average_point + 0.5 * (point1 - average_point)
print("Average point with relatively equal distances:", average_point)
字符串
4条答案
按热度按时间wljmcqd81#
你也可以用代数方法来解决这个问题,而不是通过几何或优化/最小化。
如果这些点是不同的,并且位于一个球体上,那么它们有一个唯一的等距点。对于不同的点,这是可以的,除非至少有三个点位于一条线上(原始帖子中的情况)或者所有四个点都是共面的。
设中心为(a,b,c),半径为R,则四个点(x,y,z)中的每一个满足
x1c 0d1x的数据
它会增殖并重新排列,
的
现在,为了消除R(以及中心坐标a,B,c中的二次项),从点0减去点1的方程,从点0减去点2的方程,从点0减去点3的方程。你得到三个方程,可以写成矩阵方程
的
求解(例如通过高斯消去法)中心(a,b,c),然后代入任何原始方程得到R。
如果矩阵不可逆,也就是说矩阵的行列式为零,则解失败。如果两行是彼此的倍数(直线上的三个点),或者一行是其他两行的线性组合(点共面),则会发生这种情况。
字符串
对于@aerobiomat提供的案例:
型
编辑对于那些喜欢库例程的人
如果你更喜欢使用numpy矩阵求解器,那么它要短得多:
型
ergxz8rk2#
这个问题一般没有解,但可以使用最小化方法获得近似值。
如果这些点是:
字符串
误差函数可以定义为距离的方差:
型
然后,最佳点
p
可以通过最小化误差函数来找到:型
距离是:
型
mcdcgff03#
为什么要使用循环?让我向你展示如何在2D中做到这一点:
的数据
你需要找到点
A
,B
和C
之间相同距离的点。为了做到这一点,你需要A
和B
的垂直平分线,你还需要A
和C
的垂直平分线,两者的交点给你点D
,这就是解决方案。为了证明后者,我们以
D
为圆心画了一个圆,正如你所看到的,这个圆包含了点A
、B
和C
,证明了D
与这三个起点的距离是相同的。只要将这个推理扩展到3D,你就有了解决方案,正如你所看到的,不需要循环。
yvt65v4c4#
这个基于两个平分线相交的方法不太好用。@Dominique你能看一下吗?
字符串
其结果
型