我有一个定义为xyz向量的平面和一个位于平面上的点。
我想在定义的距离/半径(r
(下,在定义点(centroid
(周围的平面上生成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))