在 3D space 中找到与质心等距的 4 个点,其中所有点都位于预定义的平面上
Find 4 points equidistant from a centroid in 3D space where all points lie on a predefined plane
我有一个定义为 xyz 向量的平面和一个位于平面上的点。
我想在定义的点 (centroid
) 周围的平面上以定义的 distance/radius (r
) 为 4 个点 (N_points
) 生成 xyz 坐标).
我当前的解决方案仅适用于 2D。我想扩展它以在 3D 中工作,但我的几何知识让我失望了。任何想法将不胜感激。
def circlePoints(r, N_points, plane=(1,1,1), centroid=(0,0,0), rotation=0):
(plane_x, plane_y, plane_z) = plane
(centroid_x, centroid_y, centroid_z) = centroid
step = (np.pi*2) / N_points
rot=rotation
i=0
points=[]
for i in xrange(N_points):
x = round(centroid_x + ((np.sin(rot)*r) / plane_x), 2)
y = round(centroid_y + ((np.cos(rot)*r) / plane_y), 2)
z=0 #?
points.append((x,y,z))
rot+=step
return points
print circlePoints(1, 4, [1,2,0], [2,3,1])
print circlePoints(1, 4)
我们需要找到两个垂直于plane
(法线)的向量。我们可以通过以下程序来完成:
- 规范化
plane
- 设置一个向量
k = (1, 0, 0)
- 计算
math.abs(np.dot(k, plane))
- 如果 > 0.9 则设置
k = (0, 1, 0)
- 计算
a = np.cross(k, plane))
和b = np.cross(plane, a)
- 你现在在平面上有两个向量。您可以通过将这两个向量相加一些数字并添加到
centeroid
来获得平面上的任何点
- 如果你想要特定的距离,你需要标准化
a
和b
代码:
import numpy as np
import math
def normalize(a):
b = 1.0 / math.sqrt(np.sum(a ** 2))
return a * b
def circlePoints(r, N_points, plane=(1,1,1), centroid=(0,0,0)):
p = normalize(np.array(plane))
k = (1, 0, 0)
if math.fabs(np.dot(k, p)) > 0.9:
k = (0, 1, 0)
a = normalize(np.cross(k, p))
b = normalize(np.cross(p, a))
step = (np.pi * 2) / N_points
ang = [step * i for i in xrange(N_points)]
return [(np.array(centroid) + \
r * (math.cos(rot) * a + math.sin(rot) * b)) \
for rot in ang]
print circlePoints(10, 5, (1, 1, 1), (0, 0, 0))
我有一个定义为 xyz 向量的平面和一个位于平面上的点。
我想在定义的点 (centroid
) 周围的平面上以定义的 distance/radius (r
) 为 4 个点 (N_points
) 生成 xyz 坐标).
我当前的解决方案仅适用于 2D。我想扩展它以在 3D 中工作,但我的几何知识让我失望了。任何想法将不胜感激。
def circlePoints(r, N_points, plane=(1,1,1), centroid=(0,0,0), rotation=0):
(plane_x, plane_y, plane_z) = plane
(centroid_x, centroid_y, centroid_z) = centroid
step = (np.pi*2) / N_points
rot=rotation
i=0
points=[]
for i in xrange(N_points):
x = round(centroid_x + ((np.sin(rot)*r) / plane_x), 2)
y = round(centroid_y + ((np.cos(rot)*r) / plane_y), 2)
z=0 #?
points.append((x,y,z))
rot+=step
return points
print circlePoints(1, 4, [1,2,0], [2,3,1])
print circlePoints(1, 4)
我们需要找到两个垂直于plane
(法线)的向量。我们可以通过以下程序来完成:
- 规范化
plane
- 设置一个向量
k = (1, 0, 0)
- 计算
math.abs(np.dot(k, plane))
- 如果 > 0.9 则设置
k = (0, 1, 0)
- 计算
a = np.cross(k, plane))
和b = np.cross(plane, a)
- 你现在在平面上有两个向量。您可以通过将这两个向量相加一些数字并添加到
centeroid
来获得平面上的任何点
- 如果你想要特定的距离,你需要标准化
a
和b
代码:
import numpy as np
import math
def normalize(a):
b = 1.0 / math.sqrt(np.sum(a ** 2))
return a * b
def circlePoints(r, N_points, plane=(1,1,1), centroid=(0,0,0)):
p = normalize(np.array(plane))
k = (1, 0, 0)
if math.fabs(np.dot(k, p)) > 0.9:
k = (0, 1, 0)
a = normalize(np.cross(k, p))
b = normalize(np.cross(p, a))
step = (np.pi * 2) / N_points
ang = [step * i for i in xrange(N_points)]
return [(np.array(centroid) + \
r * (math.cos(rot) * a + math.sin(rot) * b)) \
for rot in ang]
print circlePoints(10, 5, (1, 1, 1), (0, 0, 0))