本文介绍了如何创建与坐标平面垂直的线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个表示平面的坐标(X,Y和X)数据集(numpy数组).我需要创建一条垂直于平面的线.
I have three datasets (numpy arrays) of coordinates (X, Y and X) representing a planar surface. I need to create a line that is perpendicular to the plane surface.
我从另一个文件中获取了数据,因为我无法轻松地与您共享数据,所以我创建了一个随机的熊猫数据集,用于生成数据表面的代码如下:
I got the data from another file, because I can't share it easily with you, I created a random pandas dataset the code I used to generate the data surface is as following:
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]
如果有人能帮助我,我将不胜感激.
I would be very grateful if anyone can help me.
推荐答案
您可以使用任意两个非共线点的相对位置的叉积:
You can use the cross product of the relative position of any two non-colinear points:
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]
这篇关于如何创建与坐标平面垂直的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!