% tkz-obj-eu-sectors.tex
% Copyright 2023  Alain Matthes
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
% This work has the LPPL maintenance status “maintained”.
% The Current Maintainer of this work is Alain Matthes.

\def\fileversion{5.02c}
\def\filedate{2023/02/03} 
\typeout{2023/02/03 5.02c tkz-obj-eu-sectors.tex} 
\makeatletter  
%<-----------------------    Sectors         ------------------------------–>
\gdef\tkz@nums{0}
\pgfkeys{/drawsector/.cd,
  towards/.code               = {\def\tkz@nums{0}},
  rotate/.code                = {\def\tkz@nums{1}},
  R/.code                     = {\def\tkz@nums{2}},
  R with nodes/.code          = {\def\tkz@nums{3}},
  towards,
  /drawsector/.search also  = {/tikz},
} 

\def\tkzDrawSector{\pgfutil@ifnextchar[{\tkz@DrawSector}{\tkz@DrawSector[]}}
\def\tkz@DrawSector[#1](#2,#3)(#4){% 
\begingroup
\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
 \tkz@@CalcLengthcm(#2,#3){tkz@radius}
 \tkzFindSlopeAngle(#2,#3)\tkzGetAngle{tkz@FirstAngle}  
 \tkzFindSlopeAngle(#2,#4)\tkzGetAngle{tkz@SecondAngle} 
 \tkz@DrawSectorRAngles[#1](#2,\tkz@radius)(\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
    \tkz@@CalcLengthcm(#2,#3){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)(\tkz@FirstA,\tkz@SecondA)    
  \else
  \tkz@DrawSectorRAngles[#1](#2,\tkz@radius)(\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
    \tkz@@CalcLengthcm(#2,#3){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
    \tkz@@CalcLengthcm(#3,#4){tkz@radius}
    \tkz@DrawSectorRAngles[#1](#2,\tkz@radius pt)(#5,#6)
\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
%<--------------------------------------------------------------------------–>
\gdef\tkz@nums{0}
\pgfkeys{/@fillsector/.cd,
      towards/.code               = {\def\tkz@nums{0}},
      rotate/.code                = {\def\tkz@nums{1}},
      R/.code                     = {\def\tkz@nums{2}},
      R with nodes/.code          = {\def\tkz@nums{3}},
      towards,
      /@fillsector/.search also={/tikz},
} 

\def\tkzFillSector{\pgfutil@ifnextchar[{\tkz@FillSector}{\tkz@FillSector[]}}
\def\tkz@FillSector[#1](#2,#3)(#4){% 
\begingroup
\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}{0}
  \edef\tkz@FirstAngle{\pgfmathresult}%
  \pgfmathadd{\tkz@SecondAngle}{0}
  \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  
  \tkz@@CalcLength(#2,#3){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
    \tkz@@CalcLength(#2,#3){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\tkzFillSectorAngles{\pgfutil@ifnextchar[{\tkz@FillSectorAngles}{%
                                              \tkz@FillSectorAngles[]}}
\def\tkz@FillSectorAngles[#1](#2,#3)(#4,#5){%
\begingroup
    \tkz@@CalcLength(#2,#3){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
    \tkz@@CalcLength(#1,#2){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  
  \tkz@@CalcLength(#1,#2){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               = {\def\tkz@numcs{0}},
      rotate/.code                = {\def\tkz@numcs{1}},
      R/.code                     = {\def\tkz@numcs{2}}, 
}
%<--------------------------------------------------------------------------–>
\def\tkzClipSector{\pgfutil@ifnextchar[{\tkz@ClipSector}{%
                                        \tkz@ClipSector[]}}

\def\tkz@ClipSector[#1](#2,#3)(#4){%
\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 
} 
\makeatother
\endinput 