如何使用8个点形成一个平面?

How to form a plane surfaces using 8 points?

我已经用长方体的中心绘制了我的 8 个角点,如图 所示。

我试过散点图,但现在我想要连接这 8 个点的曲面图。

当我尝试使用曲面图时,我无法处理它。你能提出任何解决方案吗

l = 0.3 
w = 0.4
h = 0.1
center = 
 [2.10737, -0.100085, 0.716869]
F= 
   [[array([[1.]]) array([[-0.001]]) array([[-0.017]])]
 [array([[0.]]) array([[-0.999]]) array([[0.037]])]
 [array([[0.017]]) array([[0.037]]) array([[0.999]])]]


def cuboid(center, size):
    ox, oy, oz = center
    l, w, h = size

    ax = fig.gca(projection='3d') ##plot the project cuboid

    X=[ox-l/2,ox-l/2,ox-l/2,ox-l/2,ox+l/2,ox+l/2,ox+l/2,ox+l/2]
    Y=[oy+w/2,oy-w/2,oy-w/2,oy+w/2,oy+w/2,oy-w/2,oy-w/2,oy+w/2]
    Z=[oz-h/2,oz-h/2,oz+h/2,oz+h/2,oz+h/2,oz+h/2,oz-h/2,oz-h/2]

    X_new = ([])
    Y_new = ([])
    Z_new = ([])

    for i in range(0,8):

        c=np.matrix([[X[i]],
                    [Y[i]],
                    [Z[i]]])
        u=F*c

        X_new = np.append(X_new, u.item(0))
        Y_new = np.append(Y_new, u.item(1))
        Z_new = np.append(Z_new, u.item(2))

    ax.scatter(X_new,Y_new,Z_new,c='darkred',marker='o')  #the  plot of points after rotated
    ax.scatter(ox,oy,oz,c='crimson',marker='o')  #the previous plot of points before rotated



    ## Add title 
    plt.title('Plot_for_PSM', fontsize=20)
    plt.gca().invert_yaxis()

    ##labelling the axes
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')


这是一个解决方案。

###Added import
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection

###Addded size and center in this format in order to manipulate them easily
size = [0.3, 0.4, 0.1]
center = [2.10737, -0.100085, 0.716869]

###This numpy vector will be used to store the position of the sides
side = np.zeros((8,3))

###Just re-ordered your matrix in some np.arrays
F = [[np.array([1., -0.001, -0.017])],
    [np.array([0., -0.999, 0.037])],
    [np.array([0.017, 0.037, 0.999])]]

def cuboid(center, size):
    ox, oy, oz = center
    l, w, h = size

    ###Added the fig in order to be able to plot it later
    fig = plt.figure()
    ax = fig.gca(projection='3d') ##plot the project cuboid

    X=[ox-l/2,ox-l/2,ox-l/2,ox-l/2,ox+l/2,ox+l/2,ox+l/2,ox+l/2]
    Y=[oy+w/2,oy-w/2,oy-w/2,oy+w/2,oy+w/2,oy-w/2,oy-w/2,oy+w/2]
    Z=[oz-h/2,oz-h/2,oz+h/2,oz+h/2,oz+h/2,oz+h/2,oz-h/2,oz-h/2]

    X_new = ([])
    Y_new = ([])
    Z_new = ([])

    for i in range(0,8):

        c=np.matrix([[X[i]],
                    [Y[i]],
                    [Z[i]]])
        u=F*c

        X_new = np.append(X_new, u.item(0))
        Y_new = np.append(Y_new, u.item(1))
        Z_new = np.append(Z_new, u.item(2))

        ###Doing a dot product between F and c like you did earlier but using np.dot as we're now working with Numpy format
        side[i,:] = np.dot(F, c)

    ###Storing the position of every points
    sides = [[side[0],side[1],side[2],side[3]],
            [side[4],side[5],side[6],side[7]], 
            [side[0],side[1],side[4],side[5]], 
            [side[2],side[3],side[4],side[5]], 
            [side[1],side[2],side[5],side[6]],
            [side[4],side[7],side[0],side[3]]]


    ###Scatter plot 
    ax.scatter(X_new,Y_new,Z_new,c='darkred',marker='o')  #the  plot of points after rotated
    ax.scatter(ox,oy,oz,c='crimson',marker='o')  #the previous plot of points before rotated



    ### Add title 
    plt.title('Plot_for_PSM', fontsize=20)
    plt.gca().invert_yaxis()

    ##labelling the axes
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')

    ###This draw the plane sides as you wanted
    ax.add_collection3d(Poly3DCollection(sides, facecolors='blue', linewidths=1, edgecolors='r', alpha=.25))


cuboid(center, size)
###Mandatory to plot the cube
plt.show()

它使用mpl_toolkits中的Poly3DCollection, Line3DCollection画出6个平面正方形,代表立方体的边。

第一步是找到每边的4个坐标。然后你需要使用 Poly3DCollection 来绘制它。