%
%  Klasse für Arrow-Annotation-Objekte
%
classdef ShAnnot < ShLines
  properties (Access = protected)
    headX = 0;   % Position des Kopfes
    headY = 0;
    HeadStyle  = 'vback2';
    HeadLength = 10.0;
  end
    
  methods
    function obj = ShAnnot( headX, headY, varargin )
      posX = 0;
      posY = 0;
      if( nargin == 4 )  % optional posX, posY 
        posX = varargin{1};
        posY = varargin{2};
      end
      obj@ShLines(posX,posY); 
      obj.headX = headX;
      obj.headY = headY;
      obj.hnd   = [];      
      obj.LineStyle = 'vback2';
    end
    
    function setHeadStyle( obj, hs )
      if( ~isShLnObj(obj) ) return; end
      obj.HeadStyle = hs;   % Daten merken für nächsten Plot 
      hnd = obj.hnd;
      AnnHeadStyle.set( hnd, hs );      
    end
    
    function disp( obj )
      disp@Shape(obj);
      fprintf( 'headX = %g, headY = %g \n', obj.headX, obj.headY );
    end
  
    function draw( obj )      
      clear( obj );    % Methode von Shape      
      % relative Koordinaten berechnen
      aF = calcAnnotPos( [obj.x, obj.y] );
      aH = calcAnnotPos( [obj.headX, obj.headY] );
      x = [aF(1),aH(1)];
      y = [aF(2),aH(2)];
      % Plotten und MATLAB Handle merken 
      obj.hnd = annotation('arrow',x,y);          
      % Lines-Eigenschaften setzen
      obj.setLnData();
      obj.setHeadStyle(obj.HeadStyle);
    end     
    
  end  % methods 
  
end  % classdef 


% lokale Funktion 
function aP = calcAnnotPos( pos )
  v = axis;
  x0 = v(1);
  x1 = v(2);
  y0 = v(3);
  y1 = v(4);
  % relativer Punkt
  rP = [ pos(1) - x0, pos(2) - y0 ];
  % Fensterbreite
  dx = x1-x0;
  dy = y1-y0;
  % relative Koordinaten
  aP = [ rP(1)/dx, rP(2)/dy ]; 
  
  % Anfang war außerhalb    
  if( aP(1) < 0 )
    aP(1) = 0;
    fprintf( 'Annot-Anfang x außerhalb \n' );
  end
  if( aP(2) < 0 )
    aP(2) = 0;
    fprintf( 'Annot-Anfang y außerhalb \n' );
  end
  
  if( aP(1) > 1 )
    aP(1) = 1;
    fprintf( 'Annot-Ende x außerhalb \n' );
  end
  if( aP(2) > 1 )
    aP(2) = 1;
    fprintf( 'Annot-Ende y außerhalb \n' );
  end
  
end
