% 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}   

%<--------------------------------------------------------------------------–>
%<-----------------------    Sectors         ------------------------------–>
%<--------------------------------------------------------------------------–>
\tikzset{sstyle/.style={#1}}


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

\def\tkzDrawSector{\pgfutil@ifnextchar[{\tkz@DrawSector}{\tkz@DrawSector[]}}
\def\tkz@DrawSector[#1](#2,#3)(#4){% 
\begingroup
\pgfkeys{/drawsector/.cd,towards} 
\pgfqkeys{/drawsector}{#1}       
\ifcase\tkz@nums%
   \tkzDrawSectorN[#1](#2,#3)(#4)  
\or% 1
   \tkzDrawSectorRotate[#1](#2,#3)(#4)  
\or% 2
   \tkzDrawSectorRAngles[#1](#2,#3)(#4)
\or% 3
   \tkzDrawSectorR[#1](#2,#3)(#4)
\fi    
\endgroup
}


\def\tkzDrawSectorRAngles{\pgfutil@ifnextchar[{\tkz@DrawSectorRAngles}{%
                                               \tkz@DrawSectorRAngles[]}} 
\def\tkz@DrawSectorRAngles[#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
  \draw[shift = {(#2)},/drawsector/.cd,#1]%
    (#2) --(\tkz@FirstAngle:#3) arc (\tkz@FirstAngle:\tkz@SecondAngle:#3)--cycle;
 \endgroup
  } %  revoir il y a plus simple que ce shift !!! 
  %<--------------------------------------------------------------------------–>
% \tkzDrawSectorN(O,A)(B)
%<--------------------------------------------------------------------------–>
\def\tkzDrawSectorN{\pgfutil@ifnextchar[{\tkz@DrawSectorN}{%
                                         \tkz@DrawSectorN[]}}  
\def\tkz@DrawSectorN[#1](#2,#3)(#4){%
\begingroup
 \tkzCalcLength(#2,#3)\tkzGetLength{tkz@radius}
 \tkzFindSlopeAngle(#2,#3)\tkzGetAngle{tkz@FirstAngle}  
 \tkzFindSlopeAngle(#2,#4)\tkzGetAngle{tkz@SecondAngle} 
 \tkz@DrawSectorRAngles[#1](#2,\tkz@radius pt)(\tkz@FirstAngle,%
                                               \tkz@SecondAngle)      
\endgroup
}
%<--------------------------------------------------------------------------–>
% \tkzDrawSectorRotate(O,A)(angle de rotation) ??? pb avec angle negatif
%<--------------------------------------------------------------------------–>
\def\tkzDrawSectorRotate{\pgfutil@ifnextchar[{\tkz@DrawSectorRotate}{%
                                              \tkz@DrawSectorRotate[]}} 
\def\tkz@DrawSectorRotate[#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@DrawSectorRAngles[#1](#2,\tkz@radius pt)(\tkz@FirstA,\tkz@SecondA)    
  \else
  \tkz@DrawSectorRAngles[#1](#2,\tkz@radius pt)(\tkz@SecondA,\tkz@FirstA)      
  \fi        
\endgroup
}   
%<--------------------------------------------------------------------------–>
%  \tkzDrawSectorAngles(O,A)(ang1,ang2)
%<--------------------------------------------------------------------------–>
\def\tkzDrawSectorAngles{\pgfutil@ifnextchar[{\tkz@DrawSectorAngles}{%
                                              \tkz@DrawSectorAngles[]}}
\def\tkz@DrawSectorAngles[#1](#2,#3)(#4,#5){%
\begingroup
    \tkzCalcLength(#2,#3)\tkzGetLength{tkz@radius} 
    \tkz@DrawSectorRAngles[#1](#2,\tkz@radius pt)(#4,#5)      
\endgroup
}  
%<--------------------------------------------------------------------------–>
%    \tkzDrawSectorRwithNodesAngles(O,A,B)(ang1,ang2)
%<--------------------------------------------------------------------------–>
\def\tkzDrawSectorRwithNodesAngles{\pgfutil@ifnextchar[{%
            \tkz@DrawSectorRwithNodesAngles}{%
            \tkz@DrawSectorRwithNodesAngles[]}}   
\def\tkz@DrawSectorRwithNodesAngles[#1](#2,#3,#4)(#5,#6){%
\begingroup
    \tkzCalcLength(#3,#4)\tkzGetLength{tkz@radius}
    \tkz@DrawSectorRAngles[#1](#2,\tkz@radius pt)(#5,#6)
\endgroup
} 
% %<--------------------------------------------------------------------------–>
% % \tkzDrawSectorRwithNodes(O,A,B)(C,D)  center O radius  AB from C to D
% %<--------------------------------------------------------------------------–>
% \def\tkzDrawSectorRwithNodes{\pgfutil@ifnextchar[{%
%             \tkz@DrawSectorRwithNodes}{%
%             \tkz@DrawSectorRwithNodes[]}}  
% \def\tkz@DrawSectorRwithNodes[#1](#2,#3,#4)(#5,#6){%
% \begingroup
%     \tkzCalcLength(#3,#4){tkz@rad}   
%     \tkzFindSlopeAngle(#2,#5)\tkzGetAngle{tkz@FirstAngle}  
%     \tkzFindSlopeAngle(#2,#6)\tkzGetAngle{tkz@SecondAngle}
%     \tkz@DrawSectorRAngles[#1](#2,\tkz@rad)(\tkz@FirstAngle,\tkz@SecondAngle)
% \endgroup   
% } 
%<--------------------------------------------------------------------------–>
%  \tkzDrawSectorR(O, rad)(A,B)
%<--------------------------------------------------------------------------–>
\def\tkzDrawSectorR{\pgfutil@ifnextchar[{\tkz@DrawSectorR}{%
                                         \tkz@DrawSectorR[]}} 
\def\tkz@DrawSectorR[#1](#2,#3)(#4,#5){%
\begingroup 
  \tkzFindSlopeAngle(#2,#4)\tkzGetAngle{tkz@FirstAngle}  
  \tkzFindSlopeAngle(#2,#5)\tkzGetAngle{tkz@SecondAngle} 
  \tkz@DrawSectorRAngles[#1](#2,#3)(\tkz@FirstAngle,\tkz@SecondAngle)  
\endgroup   
}
%<--------------------------------------------------------------------------–>
%<--------------------------------------------------------------------------–>
%                                 Fill
%<--------------------------------------------------------------------------–>
%<--------------------------------------------------------------------------–>
\global\def\tkz@nums{0}
\pgfkeys{/fillsector/.cd,
towards/.code               = {\global\def\tkz@nums{0}},
rotate/.code                = {\global\def\tkz@nums{1}},
R/.code                     = {\global\def\tkz@nums{2}},
R with nodes/.code          = {\global\def\tkz@nums{3}},
color/.code                 = {\global\def\tkz@fillcolor{#1}},
/fillsector/.unknown/.code  = {\let\searchname=\pgfkeyscurrentname
                               \pgfkeysalso{\searchname/.try=#1,
                                      /tikz/\searchname/.retry=#1}}
} 

\def\tkzFillSector{\pgfutil@ifnextchar[{\tkz@FillSector}{\tkz@FillSector[]}}
\def\tkz@FillSector[#1](#2,#3)(#4){% 
\begingroup
\pgfkeys{/fillsector/.cd,towards} 
\pgfqkeys{/fillsector}{#1}     
\ifcase\tkz@nums%
   \tkzFillSectorN[#1](#2,#3)(#4)  
\or% 1
   \tkzFillSectorRotate[#1](#2,#3)(#4)  
\or% 2
   \tkzFillSectorRAngles[#1](#2,#3)(#4)
\or% 3
   \tkzFillSectorR[#1](#2,#3)(#4)
\fi    
\endgroup
}   


\def\tkzFillSectorRAngles{\pgfutil@ifnextchar[{\tkz@FillSectorRAngles}{%
                                               \tkz@FillSectorRAngles[]}} 
\def\tkz@FillSectorRAngles[#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} 
  \path[shift  = {(#2)},/fillsector/.cd,fill=\tkz@fillcolor,#1] (#2) -- %
      (\tkz@FirstAngle:#3) arc (\tkz@FirstAngle:\tkz@SecondAngle:#3)--cycle; 
\endgroup
}   
%<--------------------------------------------------------------------------–>
% \tkzFillSectorN(O,A)(B)
%<--------------------------------------------------------------------------–>
\def\tkzFillSectorN{\pgfutil@ifnextchar[{\tkz@FillSectorN}{\tkz@FillSectorN[]}} 
\def\tkz@FillSectorN[#1](#2,#3)(#4){%
\begingroup  
  \tkzCalcLength(#2,#3)\tkzGetLength{tkz@radius}
  \tkzFindSlopeAngle(#2,#3)\tkzGetAngle{tkz@FirstAngle}  
  \tkzFindSlopeAngle(#2,#4)\tkzGetAngle{tkz@SecondAngle} 
  \tkz@FillSectorRAngles[#1](#2,\tkz@radius pt)(\tkz@FirstAngle,%
                                                \tkz@SecondAngle)    
\endgroup
}
%<--------------------------------------------------------------------------–>
% \tkzFillSectorRotate(O,A)(angle de rotation) ??? pb avec angle negatif
%<--------------------------------------------------------------------------–>
\def\tkzFillSectorRotate{\pgfutil@ifnextchar[{\tkz@FillSectorRotate}{%
                                              \tkz@FillSectorRotate[]}} 
\def\tkz@FillSectorRotate[#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@FillSectorRAngles[#1](#2,\tkz@radius pt)(\tkz@FirstA,\tkz@SecondA)    
  \else
  \tkz@FillSectorRAngles[#1](#2,\tkz@radius pt)(\tkz@SecondA,\tkz@FirstA)      
  \fi
   \endgroup
}   
%<--------------------------------------------------------------------------–>
%  \tkzFillSectorAngles(O,A)(ang1,ang2)
%<--------------------------------------------------------------------------–>
\def\tkzDrawSectorAngles{\pgfutil@ifnextchar[{\tkz@illSectorAngles}{%
                                              \tkz@illSectorAngles[]}}
\def\tkz@illSectorAngles[#1](#2,#3)(#4,#5){%
\begingroup
    \tkzCalcLength(#2,#3)\tkzGetLength{tkz@radius} 
    \tkz@FillSectorRAngles[#1](#2,\tkz@radius pt)(#4,#5)      
\endgroup
}  
%<--------------------------------------------------------------------------–>
%  \tkzFillSectorR(O, rad)(A,B)
%<--------------------------------------------------------------------------–>
\def\tkzFillSectorR{\pgfutil@ifnextchar[{\tkz@FillSectorR}{%
                                         \tkz@FillSectorR[]}} 
\def\tkz@FillSectorR[#1](#2,#3)(#4,#5){%
\begingroup 
  \tkzFindSlopeAngle(#2,#4)\tkzGetAngle{tkz@FirstAngle}  
  \tkzFindSlopeAngle(#2,#5)\tkzGetAngle{tkz@SecondAngle} 
  \tkz@FillSectorRAngles[#1](#2,#3)(\tkz@FirstAngle,\tkz@SecondAngle)  
\endgroup   
}
%<--------------------------- Clip Sector ----------------------------------–>
%
%<--------------------------------------------------------------------------–>
\def\tkz@ClipSectorRAngles(#1,#2)(#3,#4){%
\begingroup      
  \pgfmathparse{#3}\edef\tkz@FirstAngle{\pgfmathresult}% 
  \pgfmathparse{#4}\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
  \clip[shift = {(#1)}] (#1) --(\tkz@FirstAngle:#2)%
        arc(\tkz@FirstAngle:\tkz@SecondAngle:#2) -- cycle;
 \endgroup
  } %  revoir il y a plus simple que ce shift !!! 
%<--------------------------------------------------------------------------–>
%
%<--------------------------------------------------------------------------–>
\def\tkz@ClipSectorRotate(#1,#2)(#3){%
\begingroup
    \tkzCalcLength(#1,#2)\tkzGetLength{tkz@radius} 
    \tkzFindSlopeAngle(#1,#2)\tkzGetAngle{tkz@FirstA} 
    \pgfmathadd{\tkz@FirstA}{#3}
    \edef\tkz@SecondA{\pgfmathresult} 
    \pgfmathgreaterthan{#3}{0}   
  \ifdim\pgfmathresult pt=1 pt\relax% 
   \tkz@ClipSectorRAngles(#1,\tkz@radius pt)(\tkz@FirstA,\tkz@SecondA)    
  \else
  \tkz@ClipSectorRAngles(#1,\tkz@radius pt)(\tkz@SecondA,\tkz@FirstA)      
  \fi
   \endgroup 
}
%<--------------------------------------------------------------------------–>
%
%<--------------------------------------------------------------------------–>

\def\tkz@ClipSectorN(#1,#2)(#3){%
\begingroup  
  \tkzCalcLength(#1,#2)\tkzGetLength{tkz@radius}
  \tkzFindSlopeAngle(#1,#2)\tkzGetAngle{tkz@FirstAngle}  
  \tkzFindSlopeAngle(#1,#3)\tkzGetAngle{tkz@SecondAngle} 
  \tkz@ClipSectorRAngles(#1,\tkz@radius pt)(\tkz@FirstAngle,\tkz@SecondAngle)    
\endgroup  
} 
%<--------------------------------------------------------------------------–>
%<--------------------------------------------------------------------------–>
\def\tkz@numcs{0}
\pgfkeys{
/clipsector/.cd,
towards/.code               = {\global\def\tkz@numcs{0}},
rotate/.code                = {\global\def\tkz@numcs{1}},
R/.code                     = {\global\def\tkz@numcs{2}}, 
}
%<--------------------------------------------------------------------------–>
\def\tkzClipSector{\pgfutil@ifnextchar[{\tkz@ClipSector}{%
                                        \tkz@ClipSector[]}}

\def\tkz@ClipSector[#1](#2,#3)(#4){%
\begingroup
\pgfkeys{/clipsector/.cd}
\pgfqkeys{/clipsector}{#1}
\ifcase\tkz@numcs%
   \tkz@ClipSectorN(#2,#3)(#4)
  \or% 1
   \tkz@ClipSectorRotate(#2,#3)(#4)
  \or% 2 
   \tkz@ClipSectorRAngles(#2,#3)(#4)
  \fi 
\endgroup
} 
\endinput 
 