22

I would like to plot a plane using a vector that I calculated from 3 points where:

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

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

How do I plot 'plane1' in 3D?

4
  • i believe there is a SE site for matlab. Commented Nov 19, 2012 at 23:33
  • nope, my mistake -> area51.stackexchange.com/proposals/38040/matlab Commented Nov 19, 2012 at 23:35
  • You'll most likely need to generate a bunch of points that are in the plane, and then plot those using surf or some similar function... Commented Nov 19, 2012 at 23:40
  • 3
    this might help: stackoverflow.com/questions/3461869/… Commented Nov 19, 2012 at 23:56

4 Answers 4

32

Here's an easy way to plot the plane using 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

Sign up to request clarification or add additional context in comments.

3 Comments

fill3 take X,Y,Z as input and not 3 points. Take a look at the plane you drew, it doesn't pass through (0,0,0). You drew a plane that passes through (0,-10,10), (0, -20,20) and (0,10,10)
According to Matlab documentation (2nd line for fill3) "fill3(X,Y,Z,C) fills three-dimensional polygons. X, Y, and Z triplets specify the polygon vertices". I did made a mistake though in the way I input the points to fill3 (wrong dimension used), and this is now corrected. thanks for noticing. I still think a one liner is nicer than several lines...
That's ok, you got my upvote anyway, I just wanted you to correct the mistake :)
16

You have already calculated the normal vector. Now you should decide what are the limits of your plane in x and z and create a rectangular patch.

An explanation : Each plane can be characterized by its normal vector (A,B,C) and another coefficient D. The equation of the plane is AX+BY+CZ+D=0. Cross product between two differences between points, cross(P3-P1,P2-P1) allows finding (A,B,C). In order to find D, simply put any point into the equation mentioned above:

   D = -Ax-By-Cz;

Once you have the equation of the plane, you can take 4 points that lie on this plane, and draw the patch between them.

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 Comments

@Eric, true, it should have other cases for zeroes or close to zeroes coefficients.
See my answer for another solution
2

Here's what I came up with:

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

Which you would use as:

pointA = [0; 0; 0]; % <- Use column vectors instead of the row vectors from the question
pointB = [-10; -20; 10];
pointC = [10; 20; 10];
normal = cross(pointA-pointB, pointA-pointC);
dist = dot(normal, pointA)

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

Which plots a square of side length 60 on the plane in question:

enter image description here

Comments

0

I want to add to the answer given by Andrey Rubshtein, his code works perfectly well except at B=0. Here is the edited version of his code

Below Code works when A is not 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);

Below Code works when C is not 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);

1 Comment

why do you use reOrder? @krishna-chaitanya

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.