% Copyright 2011 by Alain Matthes
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.


\def\fileversion{1.16 c}
\def\filedate{2011/06/01}   

  %<--------------------------------------------------------------------------–>
%<------------------------------  Arcs -------------------------------------–>                  
%<--------------------------------------------------------------------------–>
% options : delta 
\def\tkz@delta{0}
\tikzset{arc style/.style={#1}}
\pgfkeys{/tikz/.cd,delta/.code={\def\tkz@delta{#1}}}

\global\def\tkz@numa{0}
\pgfkeys{/drawarc/.cd,
towards/.code               = {\global\def\tkz@numa{0}},
rotate/.code                = {\global\def\tkz@numa{1}},
angles/.code                = {\global\def\tkz@numa{2}}, 
R/.code                     = {\global\def\tkz@numa{3}},
R with nodes/.code          = {\global\def\tkz@numa{4}},
/drawarc/.unknown/.code     = {\let\searchname=\pgfkeyscurrentname
                              \pgfkeysalso{\searchname/.try=#1,
                               /tikz/\searchname/.retry=#1,%
                               /pgf/\searchname/.retry=#1}}
} 

\def\tkzDrawArc{\pgfutil@ifnextchar[{\tkz@DrawArc}{\tkz@DrawArc[]}}
\def\tkz@DrawArc[#1](#2,#3)(#4){% 
\begingroup    
\pgfkeys{/drawarc/.cd,towards,delta=0} 
\pgfqkeys{/drawarc}{#1}  
\ifcase\tkz@numa%
   \tkzDrawArcN[#1](#2,#3)(#4)  
\or% 1
   \tkzDrawArcRotate[#1](#2,#3)(#4)  
\or% 2
   \tkzDrawArcAngles[#1](#2,#3)(#4) 
   \or% 3
   \tkzDrawArcRAngles[#1](#2,#3)(#4)
 \or% 4
   \tkzDrawArcR[#1](#2,#3)(#4)  
\fi    
\endgroup
}

%<--------------------------------------------------------------------------–>
% #1 center #2 radius #4 first angle (degree) #5 second angle  (degree)  
% angles  0 .. 180 or -180 .. 0
%<--------------------------------------------------------------------------–>
% example : \tkzDrawArc(A,2 cm)(30,90)    
\def\tkzDrawArcRAngles{\pgfutil@ifnextchar[{\tkz@DrawArcRAngles}{%
                                            \tkz@DrawArcRAngles[]}} 
\def\tkz@DrawArcRAngles[#1](#2,#3)(#4,#5){% 
 \begingroup    
 \pgfmathparse{#4}\edef\tkz@FirstAngle{\pgfmathresult}% 
 \pgfmathparse{#5}\edef\tkz@SecondAngle{\pgfmathresult}%  
  \pgfmathgreaterthan{\tkz@FirstAngle}{0}   
  \ifdim\pgfmathresult pt=1 pt\relax%  
    \pgfmathgreaterthan{\tkz@FirstAngle}{\tkz@SecondAngle}
    \ifdim\pgfmathresult pt=1 pt\relax%
     \pgfmathsubtract{\tkz@FirstAngle}{360}
     \edef\tkz@FirstAngle{\pgfmathresult}%
 \fi 
 \else
     \pgfmathgreaterthan{\tkz@FirstAngle}{\tkz@SecondAngle}
    \ifdim\pgfmathresult pt=1 pt\relax%
     \pgfmathadd{\tkz@SecondAngle}{360}
     \edef\tkz@SecondAngle{\pgfmathresult}%
 \fi 
 \fi
 \pgfmathsubtract{\tkz@FirstAngle}{\tkz@delta}
 \edef\tkz@FirstAngle{\pgfmathresult}%
 \pgfmathadd{\tkz@SecondAngle}{\tkz@delta}
 \edef\tkz@SecondAngle{\pgfmathresult} 
      \draw[shift = {(#2)},compass style,/drawarc/.cd,#1]%
       (\tkz@FirstAngle:#3) arc (\tkz@FirstAngle:\tkz@SecondAngle:#3);
\endgroup  
}
%<--------------------------------------------------------------------------–>
%  ARC    nodes        #2 center #3 first point last point #4
% delta un peu plus à chaque extrémité 
% example : \tkzDrawArc(A,B)(C)      
%<--------------------------------------------------------------------------–>
\def\tkzDrawArcN{\pgfutil@ifnextchar[{\tkz@DrawArcN}{\tkz@DrawArcN[]}} 
\def\tkz@DrawArcN[#1](#2,#3)(#4){%
\begingroup
  \tkzCalcLength(#2,#3)\tkzGetLength{tkz@radius}
  \tkzFindSlopeAngle(#2,#3)\tkzGetAngle{tkz@FirstAngle}  
  \tkzFindSlopeAngle(#2,#4)\tkzGetAngle{tkz@SecondAngle}
  \tkz@DrawArcRAngles[#1](#2,\tkz@radius pt)(\tkz@FirstAngle,\tkz@SecondAngle)      
\endgroup
} 
%<--------------------------------------------------------------------------–>
%    nodes                 #2 center #3 first point rotate #3 with Angle
% delta un peu plus à chaque extrémité 
% tkzDrawArcRotate(O,A)(60)
%<--------------------------------------------------------------------------–>
\def\tkzDrawArcRotate{\pgfutil@ifnextchar[{\tkz@DrawArcRotate}{%
                                           \tkz@DrawArcRotate[]}}  
\def\tkz@DrawArcRotate[#1](#2,#3)(#4){%
\begingroup
  \tkzCalcLength(#2,#3)\tkzGetLength{tkz@radius} 
  \tkzFindSlopeAngle(#2,#3)\tkzGetAngle{tkz@FirstA}
  \pgfmathadd{\tkz@FirstA}{#4} 
  \edef\tkz@SecondA{\pgfmathresult} 
    \pgfmathgreaterthan{#4}{0}   
  \ifdim\pgfmathresult pt=1 pt\relax% 
  \tkz@DrawArcRAngles[#1](#2,\tkz@radius pt)(\tkz@FirstA,\tkz@SecondA)      
  \else
  \tkz@DrawArcRAngles[#1](#2,\tkz@radius pt)(\tkz@SecondA,\tkz@FirstA)      
  \fi 
  \endgroup
}   
%<--------------------------------------------------------------------------–>
%  deux angles 
% \tkzDrawArcAngles(O,A)(0,60)
%<--------------------------------------------------------------------------–>
\def\tkzDrawArcAngles{\pgfutil@ifnextchar[{\tkz@DrawArcAngles}{%
                                           \tkz@DrawArcAngles[]}}  
\def\tkz@DrawArcAngles[#1](#2,#3)(#4,#5){%
\begingroup
    \tkzCalcLength(#2,#3) 
    \tkz@DrawArcRAngles[#1](#2,\tkzLengthResult pt)(#4,#5)      
\endgroup
}   
%<--------------------------------------------------------------------------–>
%    Degree      #2 center #4 - #3 radius from #5 (degree) to #6(degree) 
%<--------------------------------------------------------------------------–>
\def\tkzDrawArcRwithNodesAngles{\pgfutil@ifnextchar[{%
                                            \tkz@DrawArcRwithNodesAngles}{%
                                            \tkz@DrawArcRwithNodesAngles[]}} \def\tkz@DrawArcRwithNodesAngles[#1](#2,#3,#4)(#5,#6){%
\begingroup
    \tkzCalcLength(#3,#4)
    \tkz@DrawArcRAngles[#1](#2,\tkzLengthResult pt)(#5,#6)
\endgroup
}
%<--------------------------------------------------------------------------–>
%<--------------------------------------------------------------------------–>
\def\tkzDrawArcRwithNodes{\pgfutil@ifnextchar[{\tkz@DrawArcRwithNodes}{%
                                            \tkz@DrawArcRwithNodes[]}}  
\def\tkz@DrawArcRwithNodes[#1](#2,#3,#4)(#5,#6){%
\begingroup
    \tkzCalcLength(#3,#4)  
    \tkzFindSlopeAngle(#2,#5)\tkzGetAngle{tkz@FirstAngle}  
    \tkzFindSlopeAngle(#2,#6)\tkzGetAngle{tkz@SecondAngle}
    \tkz@DrawArcRAngles[#1](#2,\tkzLengthResult)%
                            (\tkz@FirstAngle,\tkz@SecondAngle)
\endgroup   
} 
%<--------------------------------------------------------------------------–>
%    Nodes R  #2 center #3 radius en cm  from #4(node) to #5(node)
%  \tkzDrawArcR(O,2 cm)(A,B)
%<--------------------------------------------------------------------------–>
\def\tkzDrawArcR{\pgfutil@ifnextchar[{\tkz@DrawArcR}{\tkz@DrawArcR[]}} 
\def\tkz@DrawArcR[#1](#2,#3)(#4,#5){%
\begingroup
   \tkzFindSlopeAngle(#2,#4)\tkzGetAngle{tkz@FirstAngle}  
   \tkzFindSlopeAngle(#2,#5)\tkzGetAngle{tkz@SecondAngle} 
   \tkz@DrawArcRAngles[#1](#2,#3)(\tkz@FirstAngle,\tkz@SecondAngle)  
\endgroup   
}
%<--------------------------------------------------------------------------–>
%<--------------------------------------------------------------------------–>
%  Author Alain Matthes  
%   idea from Y. Combe   %<--------------------------------------------------------------------------–>
%<--------------------------------------------------------------------------–>
%                Utilisation du Compas : Compass   
%<--------------------------------------------------------------------------–>
%                                Setup   Compass
%<--------------------------------------------------------------------------–>
\pgfkeys{%
setupcompass/.cd,
line width/.code   =     {\global\edef\tkz@compass@lw{#1}},
color/.code        =     {\global\edef\tkz@compass@color{#1}},
style/.code        =     {\global\edef\tkz@compass@style{#1}},
} 
%<--------------------------------------------------------------------------–>
%<--------------------------------------------------------------------------–>
\def\tkzSetUpCompass{\pgfutil@ifnextchar[{\tkzActivOff\tkz@SetUpCompass}{%
                                          \tkzActivOff\tkz@SetUpCompass[]}}
%<--------------------------------------------------------------------------–>
\def\tkz@SetUpCompass[#1]{% 
\tkzActivOff
\pgfkeys{%
setupcompass/.cd,
line width   = \tkz@euc@compasswidth,
color        = \tkz@euc@compasscolor,
style        = \tkz@euc@compassstyle
}  
\pgfqkeys{/setupcompass}{#1}
\tikzset{compass style/.style={color       = \tkz@compass@color,
                               line width  = \tkz@compass@lw,
                               style       = \tkz@compass@style
                            }}
}
%<--------------------------------------------------------------------------–> 
%                \tkzCompass(A, B)
%
% Marque de compas auto-ajustée en longueur
%               #2 center #3 le point
% Par défaut: 
%                 longueur : 1cm, mais avec un maximum d'un angle de 180°
%                 angle calculé pour la longueur
%                 style traits pleins
%
% Paramètres (optionnels, gérés par xkeyval)
%             length : longueur de l'arc (ignoré si delta est différent de 0)
%             delta  : angle sous-tendant l'arc

%<--------------------------------------------------------------------------–> 
 \pgfkeys{
    /compass/delta/.code      = {\def\tkz@delta{#1}},
    /compass/length/.code     = {\def\tkz@length{#1}},
    /compass/ratio/.code      = {\def\tkz@ratio{#1}},  
    /compass/.unknown/.code   = {\let\searchname=\pgfkeyscurrentname
                                 \pgfkeysalso{\searchname/.try=#1,
                                  /tikz/\searchname/.retry=#1}}
}
     
% \def\tkz@length{2}
% \pgfkeys{/tikz/.cd,length/.code={\def\tkz@length{#1}}}  
                                    
\def\tkzCompass{\pgfutil@ifnextchar[{\tkz@Compass}{\tkz@Compass[]}}
\def\tkz@Compass[#1](#2,#3){%   
\begingroup
\pgfkeys{%
  compass/.cd,
  length = 0.75,
  delta  = 0,
  ratio  = .5
  }
\pgfkeys{compass/.cd,#1}  
    \tkzCalcLength(#2,#3)\tkzGetLength{tkz@tempLen}  
\ifnum\tkz@delta=0 %
   \pgfmathsetmacro{\tkz@delta}{min(deg(\tkz@length cm/ \tkz@tempLen pt),180)/2}
\fi 
    \tkzFindSlopeAngle(#2,#3)\tkzGetAngle{tkz@angle}%
    \draw[shift ={(#2)},/compass/.cd,compass style,#1]%
     (\tkz@angle-\tkz@delta:\tkz@tempLen pt)%
      arc (\tkz@angle-\tkz@delta:\tkz@angle+\tkz@delta:\tkz@tempLen pt);
           %};   
\endgroup}
%<--------------------------------------------------------------------------–> 
\def\tkz@multiCompass#1 #2\@nil{% 
 \protected@edef\tkz@temp{
   \noexpand \tkzCompass[\tkz@optcompass](#1)}\tkz@temp% 
   \def\tkz@nextArg{#2}%
   \ifx\tkzutil@empty\tkz@nextArg
     \let\next\@gobble
   \fi
   \next#2\@nil
}
\def\tkzCompasss{\pgfutil@ifnextchar[{\tkz@Compasss}{\tkz@Compasss[]}}   

\def\tkz@Compasss[#1](#2){%
\global\edef\tkz@optcompass{#1} 
\begingroup
   \let\next\tkz@multiCompass
   \next#2 \@nil %    
\endgroup
}%

%<--------------------------------------------------------------------------–> 
% fin de \tkzCompass
%<--------------------------------------------------------------------------–>\endinput 
 