How to plot a plane with the given normal vector (numerically)?

2.4k Views Asked by At

I have some point and a normal vector. I want to plot a plane defined with this normal vector and a point. Analytically, we use formula (for simplicity, in 2d case):

$n_1 (x-x_0) + n_2(y-y_0) = 0$

So, we get an equation and that is it. But how can I do it in programming languages? I mean, what is an algorithm for it?

1

There are 1 best solutions below

0
On BEST ANSWER

The following might be a useful approach.

Starting with the general expression for a plane in 3D, solve for z.

$$ n_{1}\left(x-x_{0}\right)+n_{2}\left(y-y_{0}\right)+n_{3}\left(z-z_{0}\right)=0 $$

$$ n_{1}\left(x-x_{0}\right)+n_{2}\left(y-y_{0}\right)=-n_{3}\left(z-z_{0}\right) $$

$$ -\frac{n_{1}}{n_{3}}\left(x-x_{0}\right)-\frac{n_{2}}{n_{3}}\left(y-y_{0}\right)+z_{0}=z $$

Once you have this, you can generate a grid of $x,y$ values then compute the corresponding $z=f\left(x,y\right)$ and plot. The below is an example code which does this along with a figure from the code.

I hope this helps.enter image description here

import numpy
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plane parameters
n_1=2
n_2=0
n_3=1

x_0=0
y_0=0
z_0=10

# Create a grid of x and y

x = np.arange(-5, 5, 0.1)
y = np.arange(-5, 5, 0.1)
xg, yg = np.meshgrid(x, y)

# Compute z = f(x,y)

z = -n_1/n_3*(xg-x_0)-n_2/n_3*(yg-y_0)+z_0 

ax.plot_wireframe(xg, yg, z, rstride=10, cstride=10)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Plane: z=f(x,y)')
plt.show()