给定一个 N 维点 vector 。 vector 的大小为 N+1。
是否有一种通用算法可以使用球体与每个点相交的点来查找 ND 球体的中心和半径?
最佳答案
同样的问题已在 math stackexchange 上提出,并得到了建设性的答案:
这是该答案中描述的算法的 python/numpy 实现。
import numpy as np
def find_sphere_through_points(points):
n_points, n_dim = points.shape
if (n_points != n_dim + 1):
raise ValueError('Number of points must be equal to 1 + dimension')
a = np.concatenate((points, np.ones((n_points, 1))), axis=1)
b = (points**2).sum(axis=1)
x = np.linalg.solve(a, b)
center = x[:-1] / 2
radius = x[-1] + center@center
return center, radius
为了测试此方法,我们可以使用此相关问题中描述的方法在球体表面生成随机点:
import numpy as np
def sample_spherical(npoints, ndim=3, center=None):
vec = np.random.randn(npoints, ndim)
vec /= np.linalg.norm(vec, axis=1).reshape(npoints,1)
if center is None:
return vec
else:
return vec + center
n = 5
center = np.random.rand(n)
points = sample_spherical(n+1, ndim=n, center=center)
guessed_center, guessed_radius = find_sphere_through_points(points)
print('True center:\n ', center)
print('Calc center:\n ', guessed_center)
print('True radius:\n ', 1.0)
print('Calc radius:\n ', guessed_radius)
# True center:
# [0.18150032 0.94979547 0.07719378 0.26561175 0.37509931]
# Calc center:
# [0.18150032 0.94979547 0.07719378 0.26561175 0.37509931]
# True radius:
# 1.0
# Calc radius:
# 0.9999999999999997
关于c++ - 如何在给定 dims+1 点的情况下找到任意维球体的中心和半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72230142/