我如何创建与坐标平面垂直的线

问题描述 投票:0回答:1

我有三个表示平面的坐标(X,Y和X)的数据集(numpy数组)。我需要创建一条垂直于平面的线。

A visual representation of the coordinates in a coordinate system

我从另一个文件中获取了数据,因为我无法轻松地与您共享数据,所以我创建了一个随机的熊猫数据集,用于生成数据表面的代码如下:

cor_CTV = pd.DataFrame(np.random.randint(0,100,size=(100, 3)), columns = list('xyz'))

linear_data = np.c_[cor_CTV["x"], cor_CTV["y"], cor_CTV["z"]]
mn = np.min(linear_data, axis=0)
mx = np.max(linear_data, axis=0)
X,Y = np.meshgrid(np.linspace(mn[0], mx[0], 20), np.linspace(mn[1], mx[1], 20))

XX = X.flatten()
YY = Y.flatten()

A = np.c_[linear_data[:,0], linear_data[:,1], np.ones(linear_data.shape[0])]
C,_,_,_ = scipy.linalg.lstsq(A, linear_data[:,2])
Z = C[0]*X + C[1]*Y + C[2]

如果有人可以帮助我,我将非常感激。

python coordinates line surface plane
1个回答
1
投票

您可以使用任意两个非共线点的相对位置的叉积:

O = np.array([X[0][0], Y[0][0], Z[0][0]])   # Corner to be used as the origin

V1 = np.array([X[1][0], Y[1][0], Z[1][0]]) - O  # Relative vectors
V2 = np.array([X[0][1], Y[0][1], Z[0][1]]) - O

V1 = V1 / scipy.linalg.norm(V1)  # Normalise vectors
V2 = V2 / scipy.linalg.norm(V2)

# Take the cross product
perp = np.cross(V1, V2)

示例结果:

[ 0.18336919 -0.0287231  -0.98260979]
© www.soinside.com 2019 - 2024. All rights reserved.