在Mayavi中绘制两平面及其交线出错,请求排查问题根源
Hey there! Let's break down what's going wrong with your Mayavi visualization and fix it step by step:
1. Your Intersection Line Calculation is Incorrect
First off, a line in 3D can't be represented by a single linear equation (that defines a plane!). To find the line where two planes intersect, you need to:
- Compute the direction vector of the line by taking the cross product of the planes' normals:
- Plane 1 normal:
n1 = (1, -5, 2) - Plane 2 normal:
n2 = (3, -2, 3) - Direction vector:
dir_vec = n1 × n2 = (-5*3 - 2*(-2), 2*3 - 1*3, 1*(-2) - (-5)*3) = (-11, 3, 13)
- Plane 1 normal:
- Find a point that lies on both planes: Let's set
x=0and solve the system:
Solving this gives-5y + 2z = 11 -2y + 3z = 7y = -19/11andz = 13/11, so our point is(0, -19/11, 13/11).
2. Your Plane Rendering Has a Range Mismatch
In your first plane's code, you set y = np.mgrid[-100:10:1000j] — this limits the y-axis to only go up to 10, while your second plane uses -100:100:1000j. This makes the first plane cut off early, which distorts the visualization.
3. You're Generating a Point Cloud, Not a Line
When you create x and y as separate linspace arrays, you're generating all possible combinations of x and y values — that's a grid of points, not a straight line. Instead, you need to use a single parameter (like t) to compute x, y, z along the line's direction.
Fixed Code
Here's the corrected version that renders both planes and their proper intersection line:
%gui qt from mayavi import mlab import numpy as np mlab.clf() # First plane: x - 5y + 2z - 11 = 0 → z = (11 - x + 5y)/2 x, y = np.mgrid[-100:100:1000j, -100:100:1000j] # Fixed y range z = (11 - x + 5*y)/2 mlab.surf(x, y, z, warp_scale='auto', color=(0.1, 0.2, 0.3)) # Second plane: 3x - 2y + 3z -7 = 0 → z = (7 - 3x + 2y)/3 x, y = np.mgrid[-100:100:1000j, -100:100:1000j] z = (7 - 3*x + 2*y)/3 mlab.surf(x, y, z, warp_scale='auto') # Correct intersection line dir_vec = np.cross([1, -5, 2], [3, -2, 3]) point_on_line = np.array([0, -19/11, 13/11]) # Generate points along the line using parameter t t = np.linspace(-10, 10, num=400) x_line = point_on_line[0] + dir_vec[0] * t y_line = point_on_line[1] + dir_vec[1] * t z_line = point_on_line[2] + dir_vec[2] * t mlab.points3d(x_line, y_line, z_line, scale_factor=0.95, color=(1, 0, 0)) # Red line for clarity mlab.show()
This will render both full planes and a red line that correctly lies at their intersection.
内容的提问来源于stack exchange,提问作者dreamboat

