How can I plot a plane in matlab that is perpendicular to a known line and passes through another know point?

Viewed 54

I have a known line (green) that is formed by the two end points (blue point and black star). I wish to plot a plane that is perpendicular to this vector and also passes through a third point (the red one). I don't know where to start.

Here is a picture to illustrate the information I already have:

image with current information

Here is the code I am using. I am stuck both mathematically and in Matlab. I have obtained the coeffiecients of the plane but then I am not sure how to plot it.

plot3(x1,y1,z1,'.b','MarkerSize',10) %Blue point
hold on
plot3(x2,y2,z2,'kp','MarkerSize',6,'MarkerFaceColor','k') %Black star
plot3(point_x,point_y,point_z,'.r','MarkerSize',14) %Red point
plot3([x1 x2],[y1 y2],[z1 z2],'g') %Green line

A=x1-x2;
B=y1-y2;
C=z1-z2;

D=-A*point_x-B*point_y-C*point_z;
1 Answers

The following script solves your question.

If I find a way to attach compact MATLAB script to this answer I will readily make the script available.

If you send me an email I will send back the MATLAB script.

General plane equation a*X + b*Y + c*Z + d = 0

1. P1 Plane containing all 3 input points

generate random points p1 p2 p3, if all aligned move one point

clear all;close all;clc

p1=randi([-10 10],1,3)
p2=randi([-10 10],1,3)
p3=randi([-10 10],1,3)

hf1=figure(1)
ax1=gca
hold(ax1,'on')
grid on
plot3(ax1,p1(1),p1(2),p1(3),'*r','MarkerSize',10)
plot3(ax1,p2(1),p2(2),p2(3),'*g','MarkerSize',10)
plot3(ax1,p3(1),p3(2),p3(3),'*b','MarkerSize',10)

% show X Y Z axes
plot3([-10 10],[0 0],[0 0],'--r','LineWidth',.5)
plot3([0 0],[-10 10],[0 0],'--r','LineWidth',.5)
plot3([0 0],[0 0],[-10 10],'--r','LineWidth',.5)

calculate plane P1 coefficients a b c d

p123=[p1; p2; p3]
mP=mean(p123,1)
abc = null(p123 - mP)
d =10*( -p123(1,:)*abc) % d already amplified

check, should be all null

p123*abc + d

amplify coefficients

a=10*abc(1)
b=10*abc(2)
c=10*abc(3)

generate mesh

x_range=[-10:.1:10];
y_range=[-10:.1:10];
% z_range=[-10:.1:10]; % no need for z range
[X,Y]=meshgrid(x_range,y_range);

calculating plane P1 points

Z1=-1/c*(a*X+b*Y+d); 

plotting P1

hp1=surf(X, Y, Z1)
hp1.EdgeColor='none'

joining p1 p2 p3 points

plot3([p1(1) p2(1)],[p1(2) p2(2)],[p1(3) p2(3)],'c','LineWidth',3)
plot3([p1(1) p3(1)],[p1(2) p3(2)],[p1(3) p3(3)],'c','LineWidth',3)
plot3([p2(1) p3(1)],[p2(2) p3(2)],[p2(3) p3(3)],'c','LineWidth',3)

enter image description here

2. P2 perpendicular to P1 containing 2 input points

Ok, this is the plane P1 containing all 3 input points p1 p2 p3. Input points are coloured red green blue accordingly.

Let's say you are looking for, among the many possible perpedicular planes, one that contains p2 for instance. Let's call this perpendicular plane P2.

Do not use vector mP ; mP is not normal to plane P1

quiver3(0,0,0,mP(1),mP(2),mP(3),...
    'Color','#0072BD','linewidth',5,...
    'AutoScale','on','AutoScaleFactor',1,...
    'ShowArrowHead','on','Marker','.',MarkerEdgeColor='#D95319');

nP1 : normal vector to P1

nP1=[a b c]
quiver3(0,0,0,nP1(1),nP1(2),nP1(3),...
    'Color','b','linewidth',2,...
    'AutoScale','on','AutoScaleFactor',1,...
    'ShowArrowHead','on','Marker','^',MarkerEdgeColor='r');

p2 : choosen point to remain common to both planes. Now one can build vector vP21 contained in P1 with another one of the 3 input points

vP21=p2-p1

vP3=3*cross2(vP21,nP1)
vP3=3*vP3/norm(vP3)

quiver3(0,0,0,vP3(1),vP3(2),vP3(3),...
    'Color','b','linewidth',2,...
    'AutoScale','on','AutoScaleFactor',1,...
    'ShowArrowHead','on','Marker','^',MarkerEdgeColor='r');

a2=vP3(1)
b2=vP3(2)
c2=vP3(3)

d2=-sum(vP3.*p2)

Z2=-1/c2*(a2*X+b2*Y+d2); 

% P2 : plane perpendicular to P1 containing chosen point p2 and p1
hp2=surf(X, Y, Z2)
hp2.EdgeColor='none'

enter image description here

3. Perpendicular plane containing 1 input point

And here one could argue that plane P2 contains point p2 indeed but also contains another of the input points, and that the resulting perpendicular plane should only contain the chosen input point p2 excluding p1 p3.

To only keep p2 let's rotate P2 an arbitrary angle, obtaining plane P3 another perpendicular plane to P1, among many possible P1 perpendicular planes, containing p2.

% chosing a random point contained in P1
ap=randi([-10 10],1,2)
p6=[ap(1) ap(2) -1/c*(a*ap(1)+b*ap(2)+d)]

plot3(ax1,p6(1),p6(2),p6(3),'*y','MarkerSize',10)

vP26=p2-p6

vP4=3*cross2(vP26,nP1)
vP4=3*vP4/norm(vP4)

quiver3(0,0,0,vP4(1),vP4(2),vP4(3),...
    'Color','b','linewidth',2,...
    'AutoScale','on','AutoScaleFactor',1,...
    'ShowArrowHead','on','Marker','^',MarkerEdgeColor='r');

a3=vP4(1)
b3=vP4(2)
c3=vP4(3)

d3=-sum(vP4.*p2)

Z3=-1/c3*(a3*X+b3*Y+d3); 

% P3 : plane perpendicular to P1 containing chosen point p2 and p1
hp3=surf(X, Y, Z3)
hp3.EdgeColor='none'

enter image description here

4. support functions / scripts :

4.1. Paolo deLeva's cross2 available here

https://uk.mathworks.com/matlabcentral/fileexchange/8782-vector-algebra-for-arrays-of-any-size-with-array-expansion-enabled

4.2. John d'Erico's script showing compact method to go from plane points to plane coefficients in MATLAB stype; no need for a single for loop. Available here

https://uk.mathworks.com/matlabcentral/answers/723053-solve-plane-equation-with-3-points-and-additional-condition

I have amplified/attenuated supprt vectors in a convenient way, for instance I just multipled x10 all a b c d so that produced coefficients are not liliput-only values.

6. Additional Comments :

There are many perpendicular planes, even just keeping 1 input point. With additional input request like a rotation angle or a 4th point then the solution would be unique.

If you find this solution useful, would you please be so kind to mark it as correct answer?

Thanks for reading my solution.

https://uk.mathworks.com/matlabcentral/answers/723053-solve-plane-equation-with-3-points-and-additional-condition

Related