% TERNPLOT plot ternary phase diagram
%   TERNPLOT(A, B) plots ternary phase diagram for three components.  C is calculated
%      as 1 - A - B.
%
%   TERNPLOT(A, B, C) plots ternary phase data for three components A B and C.  If the values 
%       are not fractions, the values are normalised by dividing by the
%       total.
%
%   TERNPLOT(A, B, C, LINETYPE) the same as the above, but with a user specified LINETYPE (see PLOT
%       for valid linetypes).
%
%   The parameters above can be followed by parameter/value pairs to
%   specify additional properties as with the PLOT function. All
%   unrecognized options will be passed through to the PLOT command. The
%   following options are used by this function:
%
%   Parameter  Default Description
%   ---------  ------- -----------
%   majors     10      The number of major intervals to divide the plot grid into.
%   sortpoints false   Sort points in x order before plotting          
%
%   Example
%
%       plot(x, y, 'majors', 10)
%
%   NOTES
%   - An attempt is made to keep the plot close to the default plot type.  The code has been based largely on the
%     code for POLAR.       
%   - The regular TITLE and LEGEND commands work with the plot from this function, as well as incrimental plotting
%     using HOLD.  Labels can be placed on the axes using TERNLABEL
%
%   See also TERNLABEL PLOT POLAR

%       b
%      / \
%     /   \
%    c --- a 

% Author: Carl Sandrock 20020827

% To do

% Modifications
% 20160405 (SA) Added an input argument 'major'

% Modifiers
% CS Carl Sandrock
% SA Shahab Afshari

% Modified by Carlos Andres Devia

function handles = ternplot(A, B, C, varargin)

if nargin < 3
    C = 1 - (A+B);
end;

[varargin, majors] = extractpositional(varargin, 'majors', 10);
[varargin, sortpoints] = extractpositional(varargin, 'sortpoints', false);

% [varargin, majors] = extractpositional(varargin, 'majors', 10);
[varargin, axesnum] = extractpositional(varargin, 'axesnum', 'on');
[varargin, showvalue] = extractpositional(varargin, 'showvalue', 'off');
[varargin, axesint] = extractpositional(varargin, 'axesint', 'on');
[varargin, PointColors] = extractpositional(varargin, 'PointColors', nan);
[varargin, BoundaryColors] = extractpositional(varargin, 'BoundaryColors', nan);
[varargin, PointTypes] = extractpositional(varargin, 'PointTypes', {});
[varargin, BKColor] = extractpositional(varargin, 'BackgroundColor', [1 1 1]);
[varargin, PointLabel] = extractpositional(varargin, 'PointLabel', {});
[varargin, MarkerSize] = extractpositional(varargin, 'MarkerSize', {});
[varargin, labelsize] = extractpositional(varargin, 'labelsize', 20);
[varargin, ZoomOnPoints] = extractpositional(varargin, 'ZoomOnPoints', 'off');



[fA, fB, fC] = fractions(A, B, C);
[x, y] = terncoords(fA, fB, fC);

% Sort data points in x order
if sortpoints
    [x, i] = sort(x);
    y = y(i);
end

% Make ternary axes
[hold_state, cax, next] = ternaxes(majors, labelsize, 1, axesnum, axesint, BKColor);

% plot data
label_radius = 0.03;
label_angle = 0;
angle_increment = pi/length(x);
for id_point = 1:length(x)
    if isnan(PointColors)
        PointColor = [1 0 0];
    else
        if size(PointColors,1) == 1
            PointColor = PointColors;
        else
            PointColor = PointColors(id_point,:);
        end
    end

    if isnan(BoundaryColors)
        BoundaryColor = [1 0 0];
    else
        if size(BoundaryColors,1) == 1
            BoundaryColor = BoundaryColors;
        else
            BoundaryColor = BoundaryColors(id_point,:);
        end
    end

    if isempty(PointTypes)
        PointType = '.';
    else
        if size(PointTypes,1) == 1
            PointType = PointTypes{1};
        else
            PointType = PointTypes{id_point};
        end
    end

    if isempty(MarkerSize)
        q = plot3(x(id_point), y(id_point), 500, PointType, varargin{:}, 'MarkerEdgeColor', PointColor, 'MarkerFaceColor', BoundaryColor);
    else
        q = plot3(x(id_point), y(id_point), 500, PointType, 'markersize', MarkerSize(id_point), 'MarkerEdgeColor', PointColor, 'MarkerFaceColor', BoundaryColor);
    end
    hold on

    label_angle = label_angle + angle_increment + pi/4;

end

minor_x = min(x) - 0.02;
mayor_x = max(x) + 0.02;

minor_y = min(y) - 0.02;
mayor_y = max(y) + 0.02;

mid_x = mayor_x - minor_x;
mid_y = mayor_y - minor_y;

diameter = max([mid_x mid_y]);

mid_x = (mayor_x + minor_x)/2;
mid_y = (mayor_y + minor_y)/2;

minor_x = mid_x - (diameter/2);
mayor_x = mid_x + (diameter/2);

minor_y = mid_y - (diameter/2);
mayor_y = mid_y + (diameter/2);


if nargout > 0
    handles = q;
end

for id_point = 1:length(x)
    if ~isempty(PointLabel)
        text(x(id_point), y(id_point), 550, PointLabel{id_point}, 'Fontweight', 'bold', 'FontSize', 12, 'Color', [0 0 0], 'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle')
    end
end

if ~hold_state
    set(gca,'dataaspectratio',[1 1 1]), axis off; set(cax,'NextPlot',next);
end

if strcmp(ZoomOnPoints, 'on')
    axis([minor_x mayor_x minor_y mayor_y])
end
