如何在Matlab中绘制3D平面?

时间:2012-11-19 23:32:24

标签: matlab plot

我想使用我从3点计算的矢量绘制一个平面,其中:

pointA = [0,0,0];
pointB = [-10,-20,10];
pointC = [10,20,10];

plane1 = cross(pointA-pointB, pointA-pointC)

如何绘制' plane1'在3D?

4 个答案:

答案 0 :(得分:29)

这是使用fill3绘制平面的简便方法:

points=[pointA' pointB' pointC']; % using the data given in the question
fill3(points(1,:),points(2,:),points(3,:),'r')
grid on
alpha(0.3)

enter image description here

答案 1 :(得分:14)

您已经计算了法线向量。现在,您应该在xz中确定您的飞机的限制,并创建一个矩形补丁。

解释:每个平面都可以通过其法向量(A,B,C)和另一个系数D来表征。平面的方程是AX+BY+CZ+D=0。点之间的两个差异之间的交叉积cross(P3-P1,P2-P1)允许查找(A,B,C)。为了找到D,只需将任何一点放入上述等式中:

   D = -Ax-By-Cz;

一旦你得到了飞机的等式,你就可以在这个平面上取4个点,并在它们之间绘制补丁。

enter image description here

normal = cross(pointA-pointB, pointA-pointC); %# Calculate plane normal
%# Transform points to x,y,z
x = [pointA(1) pointB(1) pointC(1)];  
y = [pointA(2) pointB(2) pointC(2)];
z = [pointA(3) pointB(3) pointC(3)];

%Find all coefficients of plane equation    
A = normal(1); B = normal(2); C = normal(3);
D = -dot(normal,pointA);
%Decide on a suitable showing range
xLim = [min(x) max(x)];
zLim = [min(z) max(z)];
[X,Z] = meshgrid(xLim,zLim);
Y = (A * X + C * Z + D)/ (-B);
reOrder = [1 2  4 3];
figure();patch(X(reOrder),Y(reOrder),Z(reOrder),'b');
grid on;
alpha(0.3);

答案 2 :(得分:1)

这是我想出的:

function [x, y, z] = plane_surf(normal, dist, size)

normal = normal / norm(normal);
center = normal * dist;

tangents = null(normal') * size;

res(1,1,:) = center + tangents * [-1;-1]; 
res(1,2,:) = center + tangents * [-1;1]; 
res(2,2,:) = center + tangents * [1;1]; 
res(2,1,:) = center + tangents * [1;-1];

x = squeeze(res(:,:,1));
y = squeeze(res(:,:,2));
z = squeeze(res(:,:,3));

end

您将用作:

normal = cross(pointA-pointB, pointA-pointC);
dist = dot(normal, pointA)

[x, y, z] = plane_surf(normal, dist, 30);
surf(x, y, z);

在所讨论的平面上绘制正方形边长为60的正方形

答案 3 :(得分:0)

我想补充一下Andrey Rubshtein给出的答案,他的代码除了B = 0外完全正常。这是他的代码的编辑版本

当A不为0时<代码

normal = cross(pointA-pointB, pointA-pointC); 
x = [pointA(1) pointB(1) pointC(1)];  
y = [pointA(2) pointB(2) pointC(2)];
z = [pointA(3) pointB(3) pointC(3)];  
A = normal(1); B = normal(2); C = normal(3);
D = -dot(normal,pointA);
zLim = [min(z) max(z)];
yLim = [min(y) max(y)];
[Y,Z] = meshgrid(yLim,zLim);
X = (C * Z + B * Y + D)/ (-A);
reOrder = [1 2  4 3];
figure();patch(X(reOrder),Y(reOrder),Z(reOrder),'r');
grid on;
alpha(0.3);

当C不为0

时,以下代码有效
normal = cross(pointA-pointB, pointA-pointC); 
x = [pointA(1) pointB(1) pointC(1)];  
y = [pointA(2) pointB(2) pointC(2)];
z = [pointA(3) pointB(3) pointC(3)];  
A = normal(1); B = normal(2); C = normal(3);
D = -dot(normal,pointA);
xLim = [min(x) max(x)];
yLim = [min(y) max(y)];
[Y,X] = meshgrid(yLim,xLim);
Z = (A * X + B * Y + D)/ (-C);
reOrder = [1 2  4 3];
figure();patch(X(reOrder),Y(reOrder),Z(reOrder),'r');
grid on;
alpha(0.3);