%% $Id: pst-poly.tex 332 2010-05-28 18:45:07Z herbert $
%%
%% This is file `pst-poly.tex',
%% 
%% IMPORTANT NOTICE:
%% 
%% (C)
%% Denis Girou (CNRS/IDRIS - France) <Denis.Girou@idris.fr>
%% Herbert Voss <hvoss@tug.org>
%%
%% december 20, 2009
%%
%% This program can be redistributed and/or modified under the terms
%% of the LaTeX Project Public License Distributed from CTAN archives
%% in directory macros/latex/base/lppl.txt.
%%
%% DESCRIPTION:
%%   `pst-poly' is a PSTricks package to draw easily various kinds of regular
%%   or non regular polygons, with various customizations.
%%
%
\csname PSTPolygonLoaded\endcsname
\let\PSTPolygonLoaded\endinput
\ifx\PSTricksLoaded\endinput\else\input pstricks.tex\fi
\ifx\PSTnodesLoaded\endinput\else\input pst-node.tex\fi
\ifx\MultidoLoaded\endinput\else \input multido.tex\fi
\ifx\PSTXKeyLoaded\endinput\else \input pst-xkey \fi
%
\def\fileversion{1.62}
\def\filedate{2010/05/28}
\message{`PST-Polygon' v\fileversion, \filedate\space (DG/hv)}
%
\edef\PstAtCode{\the\catcode`\@}
\catcode`\@=11\relax
\pst@addfams{pst-poly}
\define@boolkey[psset]{pst-poly}[Pst@]{PstPicture}[true]{}
\define@key[psset]{pst-poly}{PolyRotation}[0]{\def\PstPoly@Rotation{#1}}
\define@key[psset]{pst-poly}{PolyNbSides}[5]{\pst@getint{#1}{\PstPoly@NbSides}%
  \ifnum\PstPoly@NbSides<3
    \@pstrickserr{PolyNbSides must be greater than 2
                (and not `\PstPoly@NbSides')}{\@eha}%
  \fi%
  \ifnum\PstPoly@NbSides>200
    \@pstrickserr{PolyNbSides must be less than 201
                (and not `\PstPoly@NbSides')}{\@eha}%
  \fi}
\define@key[psset]{pst-poly}{PolyOffset}[1]{\pst@getint{#1}{\PstPoly@Offset}%
  \ifnum\PstPoly@Offset<1
    \@pstrickserr{PolyOffset must be greater than 0
                (and not `\PstPoly@Offset')}{\@eha}%
  \fi}
\newdimen\PstPoly@IntermediatePointDim
\define@key[psset]{pst-poly}{PolyIntermediatePoint}[]{\def\PstPoly@IntermediatePoint{#1}}
\define@key[psset]{pst-poly}{PolyName}[]{\def\PstPoly@Name{#1}}
\define@boolkey[psset]{pst-poly}[Pst@]{PolyCurves}[true]{}
\define@boolkey[psset]{pst-poly}[Pst@]{PolyEpicycloid}[true]{}
\psset{%
  PstPicture=true,PolyRotation=0,PolyNbSides=5,PolyOffset=1,
  PolyIntermediatePoint=,PolyName=,PolyCurves=false,PolyEpicycloid=false}
%
\SpecialCoor
%
\def\PstPolygon{\pst@object{PstPolygon}}
\def\PstPolygon@i{{%
  \use@par%
  \if@star\solid@star\fi
  \ifodd\PstPoly@Offset\def\PstPoly@Decimal{.5}\else\def\PstPoly@Decimal{}\fi
  \ifPst@PstPicture\pspicture(-1,-1)(1,1)\fi
  \rput{\PstPoly@Rotation}(0,0){%
    \degrees[\PstPoly@NbSides]
    \NormalCoor
    \pssetlength{\PstPoly@IntermediatePointDim}{\PstPoly@IntermediatePoint}
    \SpecialCoor
    \ifx\PstPoly@Name\@empty
    \else
      \pnode(0,0){\PstPoly@Name 0}
      \ifnum\psxunit=\psyunit
        \def\PstPoly@Node{\pnode(1;\i)}%
      \else
        \def\PstPoly@Node{%
          \pnode(! \i\space 360 \PstPoly@NbSides\space div mul cos
                   \i\space 360 \PstPoly@NbSides\space div mul sin)}%
      \fi
      \multido{\i=0+1}{\PstPoly@NbSides}{%
        \PstPoly@Node{\PstPoly@Name\the\multidocount}}
    \fi
    \pscustom{%
      \ifPst@PolyEpicycloid
        \pst@cnta=\PstPoly@NbSides
        \divide\pst@cnta\tw@
        \multido{\i=0+1}{\PstPoly@NbSides}{%
          \moveto(1;\i)
          \lineto(1;\the\pst@cnta)
          \advance\pst@cnta\PstPoly@Offset}
      \else
        \ifnum\psxunit=\psyunit \moveto(1,0)\else\moveto(! 1 0)\fi
        \ifx\PstPoly@IntermediatePoint\@empty
          \ifnum\psxunit=\psyunit
            \def\PstPoly@Junction{\lineto(1;\i)}%
          \else
            \def\PstPoly@Junction{\lineto%
              (! \i\space 360 \PstPoly@NbSides\space div mul cos
               \i\space 360 \PstPoly@NbSides\space div mul sin)}%
          \fi
          \multido{\i=\PstPoly@Offset+\PstPoly@Offset}{\PstPoly@NbSides}{\PstPoly@Junction}
        \else
          \ifPst@PolyCurves\let\PstPoly@JunctionType\pscurve\else\let\PstPoly@JunctionType\psline\fi
          \ifnum\psxunit=\psyunit
            \def\PstPoly@Junction{\PstPoly@JunctionType%
              (\PstPoly@IntermediatePointDim;\the\pst@cnta\PstPoly@Decimal)(1;\i)}%
          \else
            \def\PstPoly@Junction{\PstPoly@JunctionType%
              (! \PstPoly@IntermediatePoint\space \the\pst@cnta\PstPoly@Decimal\space 360
                   \PstPoly@NbSides\space div mul cos mul
                 \PstPoly@IntermediatePoint\space \the\pst@cnta\PstPoly@Decimal\space 360
                   \PstPoly@NbSides\space div mul sin mul)
              (! \i\space 360 \PstPoly@NbSides\space div mul cos \i\space 360 \PstPoly@NbSides\space div mul sin)}%
          \fi
          \pst@cnta=\PstPoly@Offset
          \divide\pst@cnta\tw@
          \multido{\i=\PstPoly@Offset+\PstPoly@Offset}{\PstPoly@NbSides}{%
            \PstPoly@Junction
            \advance\pst@cnta\PstPoly@Offset}
        \fi
      \fi}
    \ifx\PstPolygonNode\@undefined
    \else\multido{\INode=\z@+\PstPoly@Offset}{\PstPoly@NbSides}{\PstPolygonNode}\fi}
  \ifPst@PstPicture\endpspicture\fi%
}}
%
\def\PstTriangle{\pst@object{PstTriangle}}
\def\PstTriangle@i{{%
  \addto@par{PolyNbSides=3,PolyRotation=90}% For triangle (360/3*(3/4))
  \use@par%
  \if@star\solid@star\fi
  \PstPolygon}}
%
\def\PstSquare{\pst@object{PstSquare}}
  \def\PstSquare@i{{%
  \addto@par{PolyNbSides=4,PolyRotation=45}% For square (360/4/2)
  \use@par%
  \if@star\solid@star\fi
  \PstPolygon}}
%
\def\PstPentagon{\pst@object{PstPentagon}}
  \def\PstPentagon@i{{%
  \addto@par{PolyNbSides=5,PolyRotation=18}% For pentagon (360/5/4)
  \use@par%
  \if@star\solid@star\fi
  \PstPolygon}}
%
\def\PstHexagon{\pst@object{PstHexagon}}
\def\PstHexagon@i{{%
  \addto@par{PolyNbSides=6}%
  \use@par%
  \if@star\solid@star\fi
  \PstPolygon}}
%
\def\PstHeptagon{\pst@object{PstHeptagon}}
\def\PstHeptagon@i{{%
  \addto@par{PolyNbSides=7,PolyRotation=38.57}% For heptagon (360/7*(3/4))
  \use@par%
  \if@star\solid@star\fi
  \PstPolygon}}
%
\def\PstOctogon{\pst@object{PstOctogon}}
\def\PstOctogon@i{{%
  \addto@par{PolyNbSides=8,PolyRotation=22.5}% For octogon (360/8/2)
  \use@par%
  \if@star\solid@star\fi
  \PstPolygon}}
%
\def\PstNonagon{\pst@object{PstNonagon}}
\def\PstNonagon@i{{%
  \addto@par{PolyNbSides=9,PolyRotation=10}% For nonagon (360/9/4)
  \use@par%
  \if@star\solid@star\fi
  \PstPolygon}}
%
\def\PstDecagon{\pst@object{PstDecagon}}
\def\PstDecagon@i{{%
  \addto@par{PolyNbSides=10}%
  \use@par%
  \if@star\solid@star\fi
  \PstPolygon}}
%
\def\PstDodecagon{\pst@object{PstDodecagon}}
\def\PstDodecagon@i{{%
  \addto@par{PolyNbSides=12,PolyRotation=15}% For dodecagon (360/12/2)
  \use@par%
  \if@star\solid@star\fi
  \PstPolygon}}
%  
\def\PstStarFiveLines{\pst@object{PstStarFiveLines}}
\def\PstStarFiveLines@i{{%
  \addto@par{PolyOffset=2,PolyRotation=18}%
  \use@par%
  \if@star\solid@star\fi
  \PstPolygon}}
%
\def\PstStarFive{\pst@object{PstStarFive}}
\def\PstStarFive@i{{%
  \addto@par{PolyIntermediatePoint=0.38,PolyRotation=18}%
  \use@par%
  \if@star\solid@star\fi
  \PstPolygon}}
%
% Aspecial version for pst-poly
\def\pst@polypyth#1#2#3{\ifdim#1>#2\pst@@polypyth#1#2#3\else\pst@@polypyth#2#1#3\fi}
\def\pst@@polypyth#1#2#3{%
     \ifdim4#1>9#2 #3=#1\advance#3 .2122#2%
     \else#3=.8384#1\advance#3 .5758#2\fi}
%
\def\pspolygonbox{\pst@object{pspolygonbox}}%   voss 2004-11-20
\def\pspolygonbox@i{\pst@makebox\pspolygonbox@ii}
\def\pspolygonbox@ii{%
   \begingroup%
   \addbefore@par{boxsep=true}%
   \pst@useboxpar%
   \pst@dima=\pslinewidth%
   \ifpsdoubleline%
     \advance\pst@dima by \psdoublesep\pslinewidth%
     \advance\pst@dima by \pslinewidth%
   \fi%
   \advance\pst@dima by \psframesep%
   \pst@dimc=\wd\pst@hbox%
   \pst@dimp=\pst@dimc 
   \advance\pst@dimp by 2\pst@dima
   \pst@dimb=\dp\pst@hbox
   \pst@dimo=\pst@dimb%
   \pst@dimd=\ht\pst@hbox
   \advance\pst@dimo by \pst@dimd% totalheight
   \pst@polypyth\pst@dimp\pst@dimo\pst@dimm
   \global\divide\pst@dimm by 2% Radius
   \setbox\pst@hbox=\hbox{%
     \ifpsboxsep\kern\pst@dima\fi% left side
     \begin@ClosedObj%
       \addto@pscode{
         /framesep \pst@number\pst@dima def
         /boxdepth \pst@number\pst@dimb framesep add def
         /boxheight0 \pst@number\pst@dimd def
         /boxheight boxheight0 framesep add def
         /boxwidth \pst@number\pst@dimc def
         /radius \pst@number\pst@dimm def
         /order \PstPoly@NbSides\space def
         /dAlpha 360 order div def
         /alpha \PstPoly@Rotation\space def
         \pst@cp
         \tx@polygonB
         \psline@iii
         \tx@Polygon
       }%
       \def\pst@linetype{1}%
       \showpointsfalse%
     \end@ClosedObj%
     \box\pst@hbox%
     \ifpsboxsep\kern\pst@dima\fi%% right side
   }%
   \ifpsboxsep%
     \pst@dimn=-.5\pst@dimo\advance\pst@dimn by \pst@dimb%
     \advance\pst@dimn by 0.809\pst@dimm%
     \dp\pst@hbox=\pst@dimn%
     \pst@dimn=\pst@dimm\advance\pst@dimn by 0.5\pst@dimo%
     \advance\pst@dimn by -\pst@dimb%
     \ht\pst@hbox=\pst@dimn\fi%
   \leavevmode%
   \box\pst@hbox%
   \endgroup%
  \ignorespaces}
%
\pst@def{polygonB}<{%
  /x1 framesep neg def
  /x2 boxwidth framesep add def
  /y1 boxdepth framesep add neg def
  /y2 boxheight framesep add def
  x2 x1 add 2 div y2 y1 add 2 div translate
  [
  alpha cos radius mul
  alpha sin radius mul
  order { /alpha alpha dAlpha add def
    radius alpha cos mul
    radius alpha sin mul
  } repeat
}>
%
\catcode`\@=\PstAtCode\relax
\endinput
%%
%% End of file `pst-poly.tex'.
