function [gx, gy, gz] = TernGrid(theAxes, nDimensions, theColor)

% TernGrid -- Grid for TernPlot.
 
% Copyright (C) 1996 Dr. Charles R. Denham, ZYDECO.
% All Rights Reserved.
 
% Version of 12-Sep-96 at 11:33:23.5.

if nargin < 1, theAxes = gca; end
if nargin < 2, nDimensions = 4; end
if nargin < 3, theColor = ':y'; end

x = []; y = []; z = [];

tx = [0 0 0; 1 2 3] ./ 4;
ty = flipud(tx);
tz = 1 - tx - ty;
x = [x tx ty tz];
y = [y ty tz tx];
z = [z tz tx ty];

if nDimensions == 4
   x = []; y = []; z = [];
   tx = [0 0 0; 1 2 3] ./ 4;
   ty = flipud(tx);
   tz = 1 - tx - ty;
   x = [x tx ty tz];
   y = [y ty tz tx];
   z = [z tz tx ty];
   tw = [1 2 3; 1 2 3] ./ 4;
   tx = [0 0 0; 3 2 1] ./ 4;
   ty = 1 - tw - tx;
   tz = 0 .* tw;
   x = [x tx ty tz];
   y = [y ty tz tx];
   z = [z tz tx ty];
   temp = tx; tx = tw; tw = temp;
   x = [x tx ty tz];
   y = [y ty tz tx];
   z = [z tz tx ty];
   temp = tx; tx = ty; ty = temp;
   x = [x tx ty tz];
   y = [y ty tz tx];
   z = [z tz tx ty];
end

if nargout < 2
   axes(theAxes)
   if nDimensions == 3
      h = TernPlot(x, y, theColor);
     else
      h = TernPlot(x, y, z, theColor);
   end
   if nargout > 0, gx = h; end
  else
   gx = x; gy = y; gz = z;
end
