## Copyright (C) 2020 Leonardo S. Borges
## 
## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see
## <https://www.gnu.org/licenses/>.

## -*- texinfo -*- 
## @deftypefn {} {} drawaxis ()
## @deftypefnx {} {} drawaxis (@var{size})
## @deftypefnx {} {} drawaxis (@var{size}, @var{angle})
## @deftypefnx {} {} drawaxis (@var{size}, @var{angle}, @var{notch})
## @deftypefnx {} {} drawaxis (@var{size}, @var{angle}, @var{notch}, @var{labels})
## @deftypefnx {} {@var{h} =} drawaxis (@dots{})
## Draw the x/y-axis in the current plot.
##
## The length of the outer-wing is given by @var{size}
## and it is a positive number,
## the angle between the line segment of the outer-wing
## and the x/y-axis is defined by @var{angle} and it is
## in the interval [0,pi/2) while
## @var{notch} controls the size of the "notch",
## @var{notch} must be in (0,1].
## If a value is not set, default values are assumed:
## @var{size}=10, @var{angle}=pi/6, @var{notch}=0.8.
## If @var{labels} are desired, they must be passed as a cell
## array.
##
## If the current figure has no axes, one will be created
## in the range [-1,1]x[-1,1].
##
## Output @var{h} is a structure with the handles of all objects (lines,
## arrow heads and labels).
##
## Example 1: plot the "x-axis" and "y-axis" in the current plot
##
## @example
## x = linspace(0,2*pi,100);
## plot(x,sin(x))
## drawaxis(10,pi/6,0.8)
## @end example
##
## Example 2: plot the "x-axis" and "y-axis" in the current plot
## and display labels "x" and "y"
##
## @example
## x = linspace(-pi,pi,100);
## plot(x,sin(x))
## drawaxis(10,pi/6,0.8,@{"x","y"@})
## @end example
##
## @seealso{plot, fill, text, line}
## @end deftypefn

## Author: LSB
## Created: 2020-05-16
## Modified: 2020-05-17
##  Added labels 
function h = drawaxis(sz,an,nc,labels)

if nargin==0
    sz = 10;
    an = pi/6;
    nc = 0.8;
elseif nargin==1
    an = pi/6;
    nc = 0.8;
elseif nargin==2
    nc = 0.8;
end

% check if variables are in correct range
% check SIZE, ANGLE and NOTCH
if sz<=0
    error("The SIZE must be positive")
end
if (an<0)||(nc>=pi/2)
    error("The ANGLE must be in the interval [0,pi/2)")
end
if (nc<=0)||(nc>1)
    error("The NOTCH must be in the interval (0,1]");
end


% get axes position in pixels
unit_old = get(gca,"units");
set(gca,"units","pixels");
axespos = get(gca,"Position");

% check if there is a plot, otherwise, create one
% from x=-1 to x=1 and y=-1 to y=1
if isempty(get(gca,"children"))
    axis([-1 1 -1 1])
end

% get axes limits
tmp = axis;
xmin = tmp(1); xmax = tmp(2); dx = xmax-xmin;
ymin = tmp(3); ymax = tmp(4); dy = ymax-ymin;
clear tmp;

% check bounds of x-axis and y-axis
if xmin*xmax < 0 
    ploty = 1;
else
    ploty = 0;
end
if ymin*ymax < 0
    plotx = 1;
else
    plotx = 0;
end


% the vertices (x,y) are store in variable "head"
headx = zeros(4,2);
heady = zeros(4,2);

% find the position of the head in pixels
% and convert to data values

% head of the arrow
headx(1,1) = xmax;
headx(1,2) = 0;

heady(1,1) = 0;
heady(1,2) = ymax;

% superior wing
px = sz*cos(pi-an)+axespos(3);
py = sz*sin(pi-an)+axespos(4)*(-ymin)/dy;
headx(2,1) = xmin + dx*px/axespos(3);
headx(2,2) = ymin + dy*py/axespos(4);

qx = sz*cos(-pi/2-an)+axespos(3)*(-xmin)/dx;
qy = sz*sin(-pi/2-an)+axespos(4);
heady(2,1) = xmin + dx*qx/axespos(3);
heady(2,2) = ymin + dy*qy/axespos(4);

% notch
headx(3,1) = headx(2,1) + (xmax-headx(2,1))*(1-nc);
headx(3,2) = 0;

heady(3,1) = 0;
heady(3,2) = heady(2,2) + (ymax-heady(2,2))*(1-nc);

% inferior wing
headx(4,1) = headx(2,1);
headx(4,2) = -headx(2,2);

heady(4,1) = -heady(2,1);
heady(4,2) = heady(2,2);

% plot the axis
holdstate = ishold;
hold on
axis_tmp = axis;

h.headx=fill(headx(:,1),headx(:,2),"k");
h.linex=line([xmin xmax],[0 0],"color","k","LineWidth",2);
if plotx == 0
    disp(" x-axis is not visible")
    disp(" ")
end

h.heady=fill(heady(:,1),heady(:,2),"k");
h.liney=line([0 0],[ymin ymax],"color","k","LineWidth",2);
if ploty == 0
    disp(" y-axis is not visible")
    disp(" ")
end
axis(axis_tmp);

% plot labels
if exist("labels","var")==1
    if iscell(labels)==1
        
        % x label
        l1 = xmin + dx*(px-1)/axespos(3);
        l2 = ymin + dy*(py-15)/axespos(4);
        h.label1=text(l1,l2,labels{1},"FontSize",12);
        
        % y label
        l3 = xmin + dx*(qx-8)/axespos(3);
        l4 = ymin + dy*(qy+1)/axespos(4);
        h.label2=text(l3,l4,labels{2},"FontSize",12);
    else
        error(" LABLES must be a cell array")
    end
end


% restore the original hold state and figure unit
if holdstate==0
    hold off
end
set(gca,"units",unit_old);

endfunction