Skip to content

Instantly share code, notes, and snippets.

@jnkather
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnkather/4b1e420f9ee0d6238737 to your computer and use it in GitHub Desktop.
Save jnkather/4b1e420f9ee0d6238737 to your computer and use it in GitHub Desktop.
Plot a plane through 3 points
% (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