Last active
August 29, 2015 14:21
-
-
Save jnkather/4b1e420f9ee0d6238737 to your computer and use it in GitHub Desktop.
Plot a plane through 3 points
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% (c) Jakob Nikolas Kather 2015 | |
% contact: http://www.kather.me | |
% this function plots a plane through the points x,y,z in a 3D plot | |
function normal = plotPlane(x,y,z) | |
% plot triangle through x y z, optional | |
% fill3([x(1),y(1),z(1)],[x(2),y(2),z(2)],[x(3),y(3),z(3)],'b') | |
% calculate normal vector | |
normal = cross((x-z),(y-z)) / norm(cross((x-z),(y-z))); | |
D = dot(normal,z); | |
% plot normal vector at origin | |
midP=zeros(1,3); | |
plot3([midP(1),midP(1)+normal(1)],[midP(2),midP(2)+normal(2)], ... | |
[midP(3),midP(3)+normal(3)],'-','LineWidth',3,'Color',[0,0,0]); | |
% plot surface in random color | |
lo = 0; hi = 1; grid = 10; | |
[X,Y] = meshgrid(linspace(lo,hi,grid), linspace(lo,hi,grid)); | |
Z=(normal(1).*X + normal(2).*Y - D)./normal(3).*(-1); | |
C=rand().*ones(size(Z)); | |
surf(X,Y,Z,C) | |
alpha(0.5) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment