function [xx, yy, zz, ww] = TernMap(x, y, z)

% TernMap -- Map of data for TernPlot.

% Copyright (C) 1996 Dr. Charles R. Denham, ZYDECO.
% All Rights Reserved.

% Version of 12-Sep-96 at 20:00:19.07.

if nargin < 1
   help TernMap
   x = 'demo';
   Begets('TernMap', 1, x)
   TernMap(x)
   return
end


if strcmp(x, 'demo')
   x = 4; y = 3;
   Begets('TernMap', 2, x, y)
   TernMap(x, y)
   return
end


if nargin < 3, z = []; end
if nargin < 2
   [m, n] = size(x);
   if n > 2, z = x(:, 3); end
   y = x(:, 2);
   x = x(:, 1);
   if nargout < 1
      Begets('TernMap', 3, x, y, z)
      TernMap(x, y, z)
     else
      [xx, yy, zz, ww] = TernMap(x, y, z);
   end
   return
end

if length(x) == 1
   m = x;
   n = 3;
   if length(y) == 1
      n = y;
   end
   x = rand(m, n-1) ./ (n-1);
   if nargout < 1
      Begets('TernMap', 1, x)
      TernMap(x)
     else
      [xx, yy, zz, ww] = TernMap(x);
   end
   return
end
      
tx = x; ty = y;
if isempty(z)
   tz = 1 - x - y;
   tw = [];
  else
   tz = z;
   tw = 1 - x - y - z;
end

if ~isempty(tw)
   factor = 0.25;   % or whatever.
   factor = 0;
   s = (tx + ty + tz) .* factor;
   tx = tx + s;
   ty = ty + s;
   tz = tz + s;
end

if nargout < 1
   disp([tx ty tz tw])
  else
   xx = tx; yy = ty; zz = tz; ww = tw;
end
