web-dev-qa-db-fra.com

Comment tracer un plan 3D dans Matlab?

Je voudrais tracer un plan en utilisant un vecteur que j'ai calculé à partir de 3 points où:

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

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

Comment tracer 'plane1' en 3D?

18
user1834916

Voici un moyen facile de tracer le plan à l'aide de 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

28
bla

Vous avez déjà calculé le vecteur normal. Vous devez maintenant décider quelles sont les limites de votre plan dans x et z et créer un patch rectangulaire.

Une explication : Chaque plan peut être caractérisé par son vecteur normal (A,B,C) et par un autre coefficient D. L'équation de l'avion est AX+BY+CZ+D=0. Le produit croisé entre deux différences entre les points, cross(P3-P1,P2-P1) permet de trouver (A,B,C). Pour trouver D, insérez simplement un point dans l’équation mentionnée ci-dessus:

   D = -Ax-By-Cz;

Une fois que vous avez l'équation du plan, vous pouvez prendre 4 points qui se trouvent sur ce plan et dessiner le patch entre eux. 

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);
14
Andrey Rubshtein

Voici ce que je suis venu avec:

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

Que vous utiliseriez comme:

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

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

Qui trace un carré de côté 60 sur l'avion en question

1
Eric

Je veux ajouter à la réponse donnée par Andrey Rubshtein, son code fonctionne parfaitement bien sauf à B = 0. Voici la version éditée de son code 

Le code ci-dessous fonctionne quand A n'est pas 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);

Le code ci-dessous fonctionne lorsque C n'est pas 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);
0
krishna chaitanya