function [h] = showplane(n,d)
% SHOWPLANE - plot a plane in R^3, given normal vector n and offset d.
%
% showplane([1,2,-3], 4) plots the plane with normal [1,2,-3] and offset d=4.
%  The plane equation is ax + by + cz + d = 0, with n = (a,b,c).
%
% Barnett 9/12/10

if numel(n)~=3, error('n must be a 3-component vector!'); end
if n(1)==0 & n(2)==0    % create orthog in-plane unit vectors u,v
  u = [1 0 0]; v = [0 1 0];
else
  u = [0 0 1]; u = u - n*dot(u,n)/norm(n)^2; u = u/norm(u);
  v = cross(n,u); v = v/norm(v);
end
r0 = -n*d/norm(n)^2; % find a point in plane
p = r0(:)*ones(1,4) + [u(:) v(:)]*[-1 -1 1 1;-1 1 1 -1];
patch(p(1,:), p(2,:), p(3,:), rand(1,3));  % random RGB color
%xlabel('x'); ylabel('y'); zlabel('z'); axis equal vis3d;
%hold on; plot3([0 u(1)], [0 u(2)], [0 u(3)], 'k-');
%plot3([0 v(1)], [0 v(2)], [0 v(3)], 'g-');
%plot3([0 n(1)], [0 n(2)], [0 n(3)], 'm-'); u, v

