如何定义一个包含3个点的平面,并以3D方式绘制?

时间:2018-12-10 02:10:59

标签: python-3.x matplotlib geometry mplot3d

我在3D空间中有3个点,我想定义一个通过3D这些点的平面。

private void monthCalendar1_SelectedDate(object sender, DateRangeEventArgs e)
{
    txtDay.Text = monthCalendar1.SelectedDate.ToString("dd");
}

我还需要在3D空间中绘制它。

1 个答案:

答案 0 :(得分:6)

您可以使用法线和点在矢量上定义平面。要找到法线,请计算由三个点定义的两个向量的叉积。

然后,使用该法线和点之一将平面放置在空间中。

使用matplotlib:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

points = [[0.65612, 0.53440, 0.24175],
           [0.62279, 0.51946, 0.25744],
           [0.61216, 0.53959, 0.26394]]

p0, p1, p2 = points
x0, y0, z0 = p0
x1, y1, z1 = p1
x2, y2, z2 = p2

ux, uy, uz = u = [x1-x0, y1-y0, z1-z0]
vx, vy, vz = v = [x2-x0, y2-y0, z2-z0]

u_cross_v = [uy*vz-uz*vy, uz*vx-ux*vz, ux*vy-uy*vx]

point  = np.array(p0)
normal = np.array(u_cross_v)

d = -point.dot(normal)

xx, yy = np.meshgrid(range(10), range(10))

z = (-normal[0] * xx - normal[1] * yy - d) * 1. / normal[2]

# plot the surface
plt3d = plt.figure().gca(projection='3d')
plt3d.plot_surface(xx, yy, z)
plt.show()

enter image description here

其他有用的答案:
Matplotlib - Plot a plane and points in 3D simultaneously
Plot a plane based on a normal vector and a point in Matlab or matplotlib