%% pgflibraryoffsetpath.code.tex
%% Copyright 2023 Jonathan Schulz
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% 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.3c or later is part of all distributions of LaTeX
% version 2008-05-04 or later.
%
% This work has the LPPL maintenance status 'maintained'.
%
% The Current Maintainer of this work is Jonathan Schulz.
%
% This work consists of the files
% pgflibrarybezieroffset.code.tex, pgflibrarynfold.code.tex, pgflibraryoffsetpath.code.tex, tikz-nfold-doc.tex, tikzlibrarynfold.code.tex, tikz-nfold-doc.tex, and tikz-nfold-doc.pdf.
%
%
% A commented version of this file can be found on https://github.com/jonschz/tikz-nfold .
%

\usepgflibrary{bezieroffset}






\gdef\pgf@nfold@cached@miterlimit{10}
\let\pgf@nfold@old@miterlimit\pgfsetmiterlimit
\def\pgfsetmiterlimit#1{%
  \pgf@nfold@old@miterlimit{#1}%
  \edef\pgf@nfold@cached@miterlimit{#1}%
}

\global\let\pgf@cached@linejoin=m%
\let\pgf@nfold@old@setbeveljoin\pgfsetbeveljoin
\let\pgf@nfold@old@setmiterjoin\pgfsetmiterjoin
\let\pgf@nfold@old@setroundjoin\pgfsetroundjoin
\def\pgfsetbeveljoin{%
  \pgf@nfold@old@setbeveljoin%
  \let\pgf@cached@linejoin=b%
}
\def\pgfsetmiterjoin{%
  \pgf@nfold@old@setmiterjoin%
  \let\pgf@cached@linejoin=m%
}
\def\pgfsetroundjoin{%
  \pgf@nfold@old@setroundjoin%
  \let\pgf@cached@linejoin=r%
}



\def\pgf@nfold@miterjoin{
  \pgfpathlineto{
    \pgfpointadd{\pgf@nfold@join@centre}{%
      \pgfpointpolar{\pgf@nfold@firstang+.5*\pgf@nfold@deltaphi+90}%
        {\pgf@nfold@shiftamount/cos(.5*\pgf@nfold@deltaphi)}%
    }%
  }%
}

\def\pgf@nfold@beveljoin{
  \pgfmathsetlengthmacro{\bevelouterprotrusion}%
    {\pgf@nfold@shortenstartjoin pt - .5*\pgflinewidth*abs(tan(.25*\pgf@nfold@deltaphi))}
  \pgfmathsetmacro{\bevelshorten}{2*\insidepercentage*abs(tan(.25*\pgf@nfold@deltaphi))}
  \pgfmathparse{\bevelshorten < abs(sin(.5*\pgf@nfold@deltaphi))}
  \ifnum\pgfmathresult=1\relax
    \pgfmathsetlengthmacro{\bevelextension}{\bevelouterprotrusion-\bevelshorten*\pgf@nfold@hwidth}
    \pgfpointadd{\pgf@nfold@join@start}{\pgfpointpolar{\pgf@nfold@firstang}{\bevelextension}}
    \pgfpathlineto{}
    \pgfpointadd{\pgf@nfold@join@end}{\pgfpointpolar{\pgf@nfold@secondang}{-\bevelextension}}
    \pgfpathlineto{}
  \else
    \pgf@nfold@miterjoin
  \fi
}

\def\pgf@nfold@roundjoin{
  \ifdim\insidepercentage pt<.5pt\relax
    \pgfpointadd{\pgf@nfold@join@start}{\pgfpointpolar{\pgf@nfold@firstang}{\pgf@nfold@shortenstartjoin}}
    \pgfpathlineto{}
    \pgfmathsetmacro\pgf@tmp@firstang{\pgf@nfold@firstang+90*\turnindicator}
    \pgfpatharc%
      {\pgf@tmp@firstang}%
      {\pgf@tmp@firstang+\pgf@nfold@deltaphi}%
      {abs(\pgf@nfold@shift@fraction)*\pgf@nfold@hwidth}%
  \else
    \pgf@nfold@miterjoin
  \fi
}

\def\pgf@nfold@token@join#1#2#3#4#5#6#7#8{%
  \def\pgf@nfold@join@centre{#1}%
  \def\pgf@nfold@join@prevend{#2}%
  \def\pgf@nfold@join@nextstart{#3}%
  \def\pgf@nfold@firstang{#4}%
  \def\pgf@nfold@secondang{#5}%
  \pgfextract@process\pgf@nfold@join@start{%
    \pgfpointadd{#2}{\pgfpointpolar{\pgf@nfold@firstang+90}{\pgf@nfold@shiftamount}}}%
  \pgfextract@process\pgf@nfold@join@end{%
    \pgfpointadd{#3}{\pgfpointpolar{\pgf@nfold@secondang+90}{\pgf@nfold@shiftamount}}}%
  \pgf@process{\pgfpointdiff{\pgf@nfold@join@start}{\pgf@nfold@join@end}}
  \pgfpointtaxicabnorm\pgfutil@tempdima
  \ifdim\pgfutil@tempdima>0.1pt\relax
    \def\pgf@nfold@deltaphi{#6}%
    \def\pgf@nfold@shortenstartjoin{#7}%
    \ifdim\pgf@nfold@deltaphi pt<0pt
      \def\turnindicator{1}
    \else
      \def\turnindicator{-1}
    \fi
    \pgfutil@tempdima=\pgf@nfold@shift@fraction pt\relax
    \pgfutil@tempdima=\turnindicator\pgfutil@tempdima
    \advance\pgfutil@tempdima by-1pt\relax
    \pgfutil@tempdima=-.5\pgfutil@tempdima
    \edef\insidepercentage{\pgf@sys@tonumber\pgfutil@tempdima}%
    #8%
  \fi
}


\def\pgf@nfold@process@join{%
  \pgf@xa=\pgf@nfold@deltaphi@start pt\relax
  \ifdim\pgf@xa<0pt\relax
    \pgf@xa=-\pgf@xa
  \fi
  \ifdim\pgf@xa>1pt\relax
    \edef\pgf@nfold@jointype{\pgf@cached@linejoin}
    \if\pgf@cached@linejoin m%
      \pgf@xa=.5\pgf@xa
      \pgfmathcos@{\pgf@sys@tonumber\pgf@xa}
      \pgf@xa=\pgfmathresult pt\relax
      \pgf@xa=\pgf@nfold@cached@miterlimit\pgf@xa
      \ifdim\pgf@xa>1pt\relax
        \def\pgf@nfold@tmp@joinmacro{\pgf@nfold@miterjoin}
      \else
        \def\pgf@nfold@tmp@joinmacro{\pgf@nfold@beveljoin}
      \fi
    \else
      \if b\pgf@cached@linejoin\relax
        \def\pgf@nfold@tmp@joinmacro{\pgf@nfold@beveljoin}
      \else
        \def\pgf@nfold@tmp@joinmacro{\pgf@nfold@roundjoin}
      \fi
    \fi
    \edef\pgf@nfold@macrotoadd{%
      \noexpand\pgf@nfold@token@join{\pgf@nfold@cur@first}{\pgf@nfold@prev@segment@end}
        {\pgf@nfold@cur@movedfirst}{\pgf@nfold@prev@angle@ii}{\pgf@nfold@cur@angle@i}%
        {\pgf@nfold@deltaphi@start}{\pgf@nfold@shortenstartjoin}{%
          \expandafter\noexpand\pgf@nfold@tmp@joinmacro%
          \expandafter\noexpand\ifpgf@nfold@closejoinsedgecase%
            \pgf@nfold@token@finish@edgecase{\pgf@nfold@cur@movedlast}%
          \else%
            \pgf@nfold@token@finish@normal%
          \fi%
        }%
    }%
    \pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
  \fi%
}


\def\pgf@nfold@token@finish@normal{%
  \pgfpathlineto{\pgf@nfold@join@end}%
}

\def\pgf@nfold@token@finish@edgecase#1{%
  \pgf@nfold@join@end%
  \ifdim\insidepercentage pt<.5pt\relax%
    \pgf@process{\pgfpointadd{#1}{\pgfpointpolar{\pgf@nfold@secondang+90}{\pgf@nfold@shiftamount}}}
  \fi%
  \pgfpathlineto{}%
}



\newif\ifpgf@nfold@continuesegment
\newif\ifpgf@nfold@closejoinsedgecase
\newif\ifpgf@nfold@angletoosharp
\newif\ifpgf@nfold@intersectionsnotloaded
\newif\ifpgf@nfold@lastconnsubpath


\def\pgf@nfold@process@segment{%
  \if\pgf@nfold@cur@visible0
    \if\pgf@nfold@cur@type m
      \if\pgf@nfold@start@arrowcode1
        \ifpgf@nfold@lastconnsubpath
          \if\pgf@nfold@prev@type f
            \if\pgf@nfold@next@visible1
              \edef\pgf@nfold@macrotoadd{%
                \noexpand\pgf@nfold@extendtotip{s}{\pgf@nfold@cur@last}{\pgf@nfold@next@angle@i}
              }%
              \pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
              \let\pgf@nfold@cur@type l
              \let\pgf@nfold@cur@visible1
              \let\pgf@nfold@cur@angle@ii\pgf@nfold@next@angle@i
              \let\pgf@nfold@cur@tang@ii\pgf@nfold@next@tang@i
              \def\pgf@nfold@deltaphi@end{0}
              \pgf@nfold@angletoosharpfalse
            \fi
          \fi%
        \fi
      \fi
    \fi%
  \else%
    \let\pgf@nfold@cur@movedfirst\pgf@nfold@cur@first
    \let\pgf@nfold@cur@movedlast\pgf@nfold@cur@last
    \def\pgf@nfold@shortenstartjoin{0}
    \def\pgf@nfold@shortenendjoin{0}
    \pgf@nfold@closejoinsedgecasefalse
    \if\pgf@nfold@prev@visible0
      \pgf@nfold@continuesegmentfalse
    \else
      \pgf@nfold@continuesegmenttrue
      \let\pgf@nfold@deltaphi@start\pgf@nfold@deltaphi@end
      \pgf@xb=\pgf@nfold@deltaphi@start pt\relax
      \ifdim\pgf@xb<0pt\relax
        \pgf@xb=-\pgf@xb
      \fi
      \ifpgf@nfold@angletoosharp\else
        \ifdim\pgf@xb>0.5pt\relax
          \pgf@yb=.5\pgf@xb
          \pgfmathtan@{\pgf@sys@tonumber\pgf@yb}
          \pgf@yb=\pgf@nfold@hwidth\relax
          \pgf@yb=\pgfmathresult\pgf@yb
          \edef\pgf@nfold@shortenstartjoin{\pgf@sys@tonumber\pgf@yb}
          \pgfextract@process\pgf@nfold@cur@movedfirst{%
            \pgfpointadd{\pgf@nfold@cur@first}%
                        {\pgfqpointscale{\pgf@nfold@shortenstartjoin}{\pgf@nfold@cur@tang@i}}}%
          \if\pgf@nfold@cur@type c
            \pgf@process{\pgfpointdiff{\pgf@nfold@cur@first}{\pgf@nfold@cur@supporta}}
            \pgfmathveclen@{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}
            \ifdim\pgfmathresult pt>0.1pt\relax
              \pgfmathveclen@{\pgfmathresult}{\pgf@sys@tonumber\pgf@yb}
              \pgfextract@process\pgf@nfold@cur@supporta{\pgfpointadd%
                {\pgf@nfold@cur@first}%
                {\pgfqpointscale{\pgfmathresult}{\pgf@nfold@cur@tang@i}}}%
            \else
              \let\pgf@nfold@cur@supporta\pgf@nfold@cur@movedfirst
            \fi
          \fi
        \fi
      \fi
    \fi
    \if\pgf@nfold@next@visible1
      \pgfmathsubtract@{\pgf@nfold@next@angle@i}{\pgf@nfold@cur@angle@ii}
      \pgf@nfold@clampangle
      \edef\pgf@nfold@deltaphi@end{\pgfmathresult}
      \pgf@xb=\pgf@nfold@deltaphi@end pt\relax
      \ifdim\pgf@xb<0pt\relax
        \pgf@xb=-\pgf@xb
      \fi
      \ifdim\pgf@xb>178pt\relax
        \pgfutil@packagewarning{tikz-nfold}{Angle too sharp, expect visual errors}
        \pgf@nfold@angletoosharptrue
      \else
        \pgf@nfold@angletoosharpfalse
        \ifdim\pgf@xb>0.5pt\relax
          \pgf@yb=.5\pgf@xb
          \pgfmathtan@{\pgf@sys@tonumber\pgf@yb}
          \pgf@yb=\pgf@nfold@hwidth\relax
          \pgf@yb=\pgfmathresult\pgf@yb
          \edef\pgf@nfold@shortenendjoin{\pgf@sys@tonumber\pgf@yb}
          \pgf@yb=-\pgf@yb
          \pgfextract@process\pgf@nfold@cur@movedlast{%
            \pgfpointadd{\pgf@nfold@cur@last}%
              {\pgfqpointscale{-\pgf@nfold@shortenendjoin}{\pgf@nfold@cur@tang@ii}}}%
          \pgfextract@process\pgf@nfold@cur@movedlast{%
            \pgfpointadd{\pgf@nfold@cur@last}%
              {\pgfqpointpolar{\pgf@nfold@cur@angle@ii}{\pgf@yb}}}%
          \if\pgf@nfold@cur@type c
            \pgf@process{\pgfpointdiff{\pgf@nfold@cur@supportb}{\pgf@nfold@cur@last}}
            \pgfmathveclen@{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}
            \ifdim\pgfmathresult pt>0.1pt\relax
              \pgfmathveclen@{\pgfmathresult}{\pgf@sys@tonumber\pgf@yb}
              \pgfextract@process\pgf@nfold@cur@supportb{\pgfpointadd%
                {\pgf@nfold@cur@last}%
                {\pgfqpointscale{-\pgfmathresult}{\pgf@nfold@cur@tang@ii}}}%
            \else
              \let\pgf@nfold@cur@supportb\pgf@nfold@cur@movedlast
            \fi
          \fi
        \fi
      \fi
      \if\pgf@nfold@cur@type l
        \pgf@process{\pgfpointdiff{\pgf@nfold@cur@first}{\pgf@nfold@cur@last}}
        \pgfmathveclen@{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}
        \pgf@xa=\pgf@nfold@shortenstartjoin pt\relax
        \advance\pgf@xa by\pgf@nfold@shortenendjoin pt\relax
        \ifdim\pgf@xa>\pgfmathresult pt\relax
          \pgf@nfold@closejoinsedgecasetrue
        \fi
      \fi
    \fi%
    \if\pgf@nfold@prev@visible0%
      \ifpgf@nfold@closejoinsedgecase
        \edef\pgf@nfold@macrotoadd{%
          \noexpand\pgf@nfold@token@edgecase@movetostart{\pgf@nfold@cur@movedfirst}{\pgf@nfold@cur@angle@i}%
        }%
        \pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
      \fi
    \else
      \ifpgf@nfold@angletoosharp\else
        \if\pgf@nfold@cur@type z\else
          \pgf@nfold@process@join
        \fi
      \fi
    \fi
    \let\pgf@nfold@prev@segment@end\pgf@nfold@cur@movedlast
    \if\pgf@nfold@cur@type i
      \let\pgf@nfold@connsubpath@tang@i\pgf@nfold@next@tang@i%
      \let\pgf@nfold@connsubpath@angle@i\pgf@nfold@next@angle@i%
    \fi
    \if\pgf@nfold@cur@type l
      \ifpgf@nfold@closejoinsedgecase\else
        \edef\pgf@nfold@macrotoadd{%
          \expandafter\noexpand\ifpgf@nfold@continuesegment%
            \pgf@nfold@token@lineto@continue%
          \else%
            \pgf@nfold@token@lineto%
          \fi{\pgf@nfold@cur@movedfirst}{\pgf@nfold@cur@movedlast}{\pgf@nfold@cur@tang@i}%
        }%
        \pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
      \fi
    \fi
    \if\pgf@nfold@cur@type o
      \def\pgf@nfold@macrotoadd{\pgf@nfold@token@closepath}%
      \pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
    \fi
    \if\pgf@nfold@cur@type z
      \def\pgf@nfold@macrotoadd{\pgf@nfold@token@closepath}%
      \pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
    \fi
    \if\pgf@nfold@cur@type c
      \ifpgf@nfold@continuesegment
        \pgf@subdividecurve{\pgf@nfold@cur@movedfirst}{\pgf@nfold@cur@supporta}{\pgf@nfold@cur@supportb}{\pgf@nfold@cur@movedlast}{\pgf@offset@max@recursion}{0}{\pgf@nfold@addcurvesegment@callback@continue}
      \else
        \pgf@subdividecurve{\pgf@nfold@cur@movedfirst}{\pgf@nfold@cur@supporta}{\pgf@nfold@cur@supportb}{\pgf@nfold@cur@movedlast}{\pgf@offset@max@recursion}{0}{\pgf@nfold@addcurvesegment@callback}%
      \fi
    \fi
    \if\pgf@nfold@cur@type i
      \edef\pgf@nfold@macrotoadd{%
        \noexpand\pgf@nfold@token@invisibleline{\pgf@nfold@cur@movedfirst}{\pgf@nfold@cur@movedlast}{\pgf@nfold@cur@tang@i}%
      }%
      \pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
    \fi
    \if\pgf@nfold@next@type t%
      \ifnum\pgf@nfold@end@arrowcode=1
        \edef\pgf@nfold@macrotoadd{%
          \noexpand\pgf@nfold@extendtotip{e}{\pgf@nfold@cur@last}{\pgf@nfold@cur@angle@ii}}%
        \pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
      \fi%
    \fi%
  \fi%
}


\def\pgf@nfold@addcurvesegment@callback#1#2#3#4#5{%
  \if#50%
    \edef\pgf@nfold@macrotoadd{%
      \noexpand\pgf@nfold@token@curveto{#1}{#2}{#3}{#4}{\pgf@tmp@tang@i}{\pgf@tmp@tang@ii}%
    }%
    \pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
  \else%
    \pgf@nfold@addcurvesegment@callback@continue{#1}{#2}{#3}{#4}{#5}%
  \fi%
}

\def\pgf@nfold@addcurvesegment@callback@continue#1#2#3#4#5{%
  \edef\pgf@nfold@macrotoadd{%
    \noexpand\pgf@nfold@token@curveto@continue{#1}{#2}{#3}{#4}{\pgf@tmp@tang@i}{\pgf@tmp@tang@ii}%
  }%
  \pgf@nfold@addmacro\pgf@parsed@cur@conn@seg%
}



\expandafter\def\csname pgf@nfold@intersec@cache@2@3\endcsname{\pgfqpoint{2pt}{0pt}}
\expandafter\def\csname pgf@nfold@intersec@cache@2@4\endcsname{\pgfqpoint{0.94063pt}{-0.33333pt}}
\expandafter\def\csname pgf@nfold@intersec@cache@3@4\endcsname{\pgfqpoint{0.94063pt}{0.33333pt}}
\expandafter\def\csname pgf@nfold@intersec@cache@2@5\endcsname{\pgfqpoint{0.64167pt}{-0.5pt}}
\expandafter\def\csname pgf@nfold@intersec@cache@3@5\endcsname{\pgfqpoint{2pt}{0pt}}
\expandafter\def\csname pgf@nfold@intersec@cache@4@5\endcsname{\pgfqpoint{0.64167pt}{0.5pt}}
\def\pgf@nfold@intersec@numcached{5}

\newif\ifpgf@nfold@ontheedge
\def\pgf@nfold@extendtotip#1#2#3{
  \ifpgf@nfold@intersectionsnotloaded
    \pgfutil@packageerror{tikz-nfold}{%
        If `nfold' is larger than \pgf@nfold@intersec@numcached\space and you use
        an `Implies' arrow tip you need to say \string\usetikzlibrary{intersections}}{}
  \else
  \pgf@nfold@ontheedgetrue
  \ifnum\pgf@nfold@index>1\relax\ifnum\pgf@nfold@index<\pgf@nfold@order\relax%
    \pgf@nfold@ontheedgefalse
  \fi\fi
  \ifpgf@nfold@ontheedge%
    \if#1s
      \pgfpathmoveto{\pgfpointadd{#2}{\pgfpointpolar{#3+90}{\pgf@nfold@shiftamount}}}%
    \fi
  \else
    \ifcsname pgf@nfold@intersec@cache@\the\pgf@nfold@index @\the\pgf@nfold@order\endcsname
      \pgfextract@process\pgf@nfold@arrowintersect
        {\csname pgf@nfold@intersec@cache@\the\pgf@nfold@index @\the\pgf@nfold@order\endcsname}%
    \else
      \pgfintersectionofpaths{
        \pgfpathmoveto{\pgfqpoint{-1.4pt}{2.65pt}}
        \pgfpathcurveto{\pgfqpoint{-0.75pt}{1.25pt}}{\pgfqpoint{1pt}{0.05pt}}{\pgfqpoint{2pt}{0pt}}
        \pgfpathcurveto{\pgfqpoint{1pt}{-0.05pt}}{\pgfqpoint{-0.75pt}{-1.25pt}}{\pgfqpoint{-1.4pt}{-2.65pt}}
      }{
        \pgfpathmoveto{\pgfqpoint{-3pt}{\pgf@nfold@shift@fraction pt}}
        \pgfpathlineto{\pgfqpoint{3pt}{\pgf@nfold@shift@fraction pt}}
      }
      \ifnum\pgfintersectionsolutions>0
        \pgfextract@process\pgf@nfold@arrowintersect{\pgfpointintersectionsolution{1}}%
        \immediate\write17{tikz-nfold: computed intersection cache@\the\pgf@nfold@index @\the\pgf@nfold@order: \string\pgfqpoint{\the\pgf@x}{\the\pgf@y}^^J}
        \expandafter\xdef\csname pgf@nfold@intersec@cache@\the\pgf@nfold@index @\the\pgf@nfold@order\endcsname{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}
      \else
        \pgfutil@packagewarning{tikz-nfold}{did not find intersection}
        \pgfextract@process\pgf@nfold@arrowintersect{\pgfqpoint{0pt}{\pgf@nfold@shift@fraction pt}}%
      \fi
    \fi%
    \begingroup
      \pgftransformreset
      \pgftransformshift{#2}
      \pgftransformrotate{#3}
      \if#1s
        \pgftransformxscale{-1}
      \fi
      \pgfutil@tempdima=\pgf@nfold@hwidth
      \pgfutil@tempdima=\pgf@nfold@shift@fraction\pgfutil@tempdima
      \pgfextract@process\pgf@nfold@startofextension
        {\pgfpointtransformed{\pgfqpoint{0pt}{\pgfutil@tempdima}}}
      \pgftransformshift{\pgfqpoint{.42\pgflinewidth}{0pt}}
      \pgftransformscale{\pgf@nfold@hwidth}
      \pgfextract@process\pgf@nfold@arrowintersect{\pgfpointtransformed{\pgf@nfold@arrowintersect}}
      \global\let\pgf@nfold@startofextension\pgf@nfold@startofextension
      \global\let\pgf@nfold@arrowintersect\pgf@nfold@arrowintersect
    \endgroup
    \if#1s
      \pgfpathmoveto{\pgf@nfold@arrowintersect}
      \pgfpathlineto{\pgf@nfold@startofextension}
    \else\if#1e
      \pgfpathlineto{\pgf@nfold@arrowintersect}
    \else
      \pgfutil@packageerror{tikz-nfold}{Invalid argument to \string\pgf@nfold@extendtotip: \meaning#1}{}
    \fi\fi
    \pgftransformreset
    \fi%
  \fi%
}


\def\pgf@nfold@start@arrowcode{0}
\def\pgf@nfold@end@arrowcode{0}

\def\pgf@nfold@parsearrows{
  \ifpgfutil@tempswa%
    \expandafter\pgf@nfold@parsearrowmacro\pgf@start@tip@sequence\relax
    \let\pgf@nfold@start@arrowcode\pgf@nfold@detectedarrow
    \expandafter\pgf@nfold@parsearrowmacro\pgf@end@tip@sequence\relax
    \let\pgf@nfold@end@arrowcode\pgf@nfold@detectedarrow
  \else
    \def\pgf@nfold@start@arrowcode{0}
    \def\pgf@nfold@end@arrowcode{0}
  \fi
}

\def\pgf@nfold@parsearrowmacro#1{%
  \def\pgf@nfold@detectedarrow{0}
  \ifx#1\relax
    \let\pgf@next\relax
  \else
    \ifx#1\pgf@arrow@handle
      \let\pgf@next\pgf@nfold@parse@arrow@handle
    \else
      \ifx#1\pgf@arrow@handle@shorthand@empty
        \let\pgf@next\pgf@nfold@parse@shorthandempty
      \else
        \let\pgf@next\pgfutil@gobble@until@relax
      \fi
    \fi
  \fi
  \pgf@next
}

\def\pgf@nfold@param@Implies{Implies}

\def\pgf@nfold@parse@arrow@handle#1{%
  \def\pgf@tmp{#1}
  \ifx\pgf@tmp\pgf@nfold@param@Implies
    \def\pgf@nfold@detectedarrow{1}
  \fi
  \pgfutil@gobble@until@relax
}

\def\pgf@nfold@parse@shorthandempty#1{
  \expandafter\def\expandafter\pgf@tmp\expandafter{#1}
  \expandafter\pgf@nfold@parsearrowmacro\pgf@tmp\relax
  \pgfutil@gobble@until@relax
}



\newcount\pgf@nfold@order
\pgf@nfold@order=1

\def\pgf@nfold@preparenfoldpath{%
  \ifnum\pgf@nfold@order>1\relax
    \ifdim\pgfinnerlinewidth>0pt\relax
      \pgfsyssoftpath@getcurrentpath\pgf@nfold@cachedpath%
      \pgfsyssoftpath@setcurrentpath\pgfutil@empty%
    \else
      \pgfutil@packageerror{tikz-nfold}{Must set \string\pgfinnerlinewidth\space to use nfold, e.g. by setting /tikz/double distance}{}
    \fi
  \fi
}

\let\pgf@nfold@old@path@check@proper\pgf@path@check@proper
\def\pgf@path@check@proper{%
  \pgf@nfold@old@path@check@proper%
  \ifpgfutil@tempswa\else%
    \pgf@nfold@preparenfoldpath%
  \fi%
}

\let\pgf@nfold@old@prepare@start@of@path\pgf@prepare@start@of@path
\def\pgf@prepare@start@of@path{%
  \pgf@nfold@old@prepare@start@of@path%
  \pgf@nfold@preparenfoldpath%
}

\let\pgf@nfold@old@stroke@inner@line\pgf@stroke@inner@line
\def\pgf@stroke@inner@line{%
  \ifnum\pgf@nfold@order>1\relax%
    \pgf@nfold@render@cached@softpath%
  \else%
    \pgf@nfold@old@stroke@inner@line%
  \fi%
}




\def\pgf@nfold@parsemoveto#1#2{%
  \ifx\pgf@cur@conn@subpath\pgfutil@empty%
    \def\pgf@nfold@parser@last@moveto{{#1}{#2}}%
    \def\pgf@cur@conn@subpath{\relax}%
  \else%
    \def\pgf@nfold@macrotoadd{\pgf@nfold@processmoveto{#1}{#2}}%
    \pgf@nfold@addmacro\pgf@cur@conn@subpath%
    \pgf@nfold@lastconnsubpathfalse%
    \pgf@nfold@process@conn@subpath%
    \let\pgf@nfold@last@closepath@from\pgfutil@empty%
    \let\pgf@cur@conn@subpath\pgfutil@empty%
    \pgf@nfold@parser@setupfirst%
    \def\pgf@nfold@parser@last@moveto{{#1}{#2}}%
  \fi%
  \pgf@nfold@parsesoftpath@finish{#1}{#2}%
}

\def\pgf@nfold@parselineto#1#2{%
  \def\pgf@nfold@macrotoadd{\pgf@nfold@processlineto{#1}{#2}}%
  \pgf@nfold@addmacro\pgf@cur@conn@subpath%
  \pgf@nfold@parsesoftpath@finish{#1}{#2}%
}

\def\pgf@nfold@parseclosepath#1#2{%
  \let\pgf@nfold@last@closepath@from\pgf@nfold@parser@previous@pt%
  \def\pgf@nfold@macrotoadd{\pgf@nfold@processclosepath{#1}{#2}}%
  \pgf@nfold@addmacro\pgf@cur@conn@subpath%
  \pgf@nfold@parsesoftpath@finish{#1}{#2}%
}

\def\pgf@nfold@parsecurveto#1#2{%
  \edef\pgf@nfold@macrotoadd{\noexpand\pgf@nfold@processcurveto\pgf@nfold@parser@supporta\pgf@nfold@parser@supportb{#1}{#2}}%
  \pgf@nfold@addmacro\pgf@cur@conn@subpath%
  \pgf@nfold@parsesoftpath@finish{#1}{#2}%
}

\def\pgf@nfold@parserect#1#2#3#4{%
  \pgf@nfold@parsemoveto{#1}{#2}%
  \pgf@xc=#1\relax
  \pgf@yc=#2\relax
  \pgf@xd=#3\relax
  \pgf@yd=#4\relax
  \advance\pgf@yc\pgf@yd%
  \edef\pgf@temp{{\the\pgf@xc}{\the\pgf@yc}}%
  \expandafter\pgf@nfold@parselineto\pgf@temp%
  \advance\pgf@xc\pgf@xd%
  \edef\pgf@temp{{\the\pgf@xc}{\the\pgf@yc}}%
  \expandafter\pgf@nfold@parselineto\pgf@temp%
  \advance\pgf@yc-\pgf@yd%
  \edef\pgf@temp{{\the\pgf@xc}{\the\pgf@yc}}%
  \expandafter\pgf@nfold@parselineto\pgf@temp%
  \advance\pgf@xc-\pgf@xd%
  \edef\pgf@temp{{\the\pgf@xc}{\the\pgf@yc}}%
  \expandafter\pgf@nfold@parseclosepath\pgf@temp%
  \pgf@nfold@parsemoveto{#1}{#2}%
}


\def\pgf@nfold@parsesoftpath#1#2{%
  \let\pgf@cur@conn@subpath\pgfutil@empty%
  \let\pgf@all@parsed@subpaths\pgfutil@empty%
  \edef\pgf@nfold@parser@last@moveto{{\the\pgf@path@lastx}{\the\pgf@path@lasty}}%
  \let\pgf@nfold@last@closepath@from\pgfutil@empty%
  \pgf@nfold@parser@setupfirst%
  \expandafter\pgf@nfold@@parsesoftpath#1\pgf@stop{}{}%
  \let#2\pgf@all@parsed@subpaths%
}%

\def\pgf@nfold@@parsesoftpath#1#2#3{%
  \let\pgf@next\pgf@nfold@@parsesoftpath%
  \ifx#1\pgf@stop%
    \def\pgf@nfold@macrotoadd{\pgf@nfold@processlast}%
    \pgf@nfold@addmacro\pgf@cur@conn@subpath%
    \pgf@nfold@lastconnsubpathtrue%
    \pgf@nfold@process@conn@subpath%
    \let\pgf@next\relax%
  \else%
    \ifx#1\pgfsyssoftpath@movetotoken%
      \pgf@nfold@parsemoveto{#2}{#3}%
    \else%
      \ifx#1\pgfsyssoftpath@linetotoken%
        \pgf@nfold@parselineto{#2}{#3}%
      \else%
        \ifx#1\pgfsyssoftpath@curvetosupportatoken%
          \def\pgf@nfold@parser@supporta{{#2}{#3}}%
        \else%
          \ifx#1\pgfsyssoftpath@curvetosupportbtoken%
            \def\pgf@nfold@parser@supportb{{#2}{#3}}%
          \else%
            \ifx#1\pgfsyssoftpath@curvetotoken%
              \pgf@nfold@parsecurveto{#2}{#3}%
            \else%
              \ifx#1\pgfsyssoftpath@closepathtoken%
                \pgf@nfold@parseclosepath{#2}{#3}%
              \else%
                \ifx#1\pgfsyssoftpath@rectcornertoken%
                  \def\pgf@nfold@parser@rectcorner{{#2}{#3}}%
                \else%
                  \ifx#1\pgfsyssoftpath@rectsizetoken%
                    \expandafter\pgf@nfold@parserect\pgf@nfold@parser@rectcorner{#2}{#3}%
                  \else%
                    \pgfutil@packageerror{tikz-nfold}{Unrecognised soft path token `#1'}{}%
                  \fi%
                \fi%
              \fi%
            \fi%
          \fi%
        \fi%
      \fi%
    \fi%
  \fi%
  \pgf@next%
}%

\def\pgf@nfold@parsesoftpath@finish#1#2{%
  \def\pgf@nfold@parser@previous@pt{{#1}{#2}}%
  \let\pgf@nfold@sectolast@segment\pgf@nfold@last@segment%
  \let\pgf@nfold@last@segment\pgf@nfold@macrotoadd%
}

\def\pgf@nfold@process@conn@subpath{%
  \let\pgf@parsed@cur@conn@seg\pgfutil@empty%
  \ifx\pgf@nfold@last@closepath@from\pgfutil@empty%
    \expandafter\pgf@nfold@processmoveto\pgf@nfold@parser@last@moveto%
  \else%
    \expandafter\pgf@nfold@processmoveto\pgf@nfold@last@closepath@from%
    \expandafter\pgf@nfold@processinvisibleline\pgf@nfold@parser@last@moveto%
  \fi%
  \pgf@cur@conn@subpath%
  \let\pgf@nfold@macrotoadd\pgf@parsed@cur@conn@seg%
  \pgf@nfold@addmacro\pgf@all@parsed@subpaths%
}


\def\pgf@nfold@processmoveto#1#2{%
  \pgf@nfold@process@moveup%
  \let\pgf@nfold@next@type=m%
  \let\pgf@nfold@next@visible=0%
  \edef\pgf@nfold@next@last{\pgf@x#1\pgf@y#2}%
  \pgf@nfold@process@segment%
}%

\def\pgf@nfold@process@line@common#1#2#3{%
  \pgf@nfold@process@moveup%
  \pgfextract@process\pgf@tmp@tang@i{\pgfpointnormalised{}\global\let\pgf@nfold@tmp\pgf@tmp}
  \let\pgf@nfold@next@type=#3%
  \let\pgf@nfold@next@visible=1%
  \let\pgf@nfold@next@tang@i\pgf@tmp@tang@i
  \let\pgf@nfold@next@tang@ii\pgf@tmp@tang@i
  \let\pgf@nfold@next@angle@i\pgf@nfold@tmp
  \let\pgf@nfold@next@angle@ii\pgf@nfold@tmp
  \let\pgf@nfold@next@first\pgf@nfold@cur@last
  \def\pgf@nfold@next@last{\pgf@x#1\pgf@y#2}%
  \pgf@nfold@process@segment%
}

\def\pgf@nfold@processlineto#1#2{%
  \pgf@process{\pgfpointdiff{\pgf@nfold@next@last}{\pgf@x#1\pgf@y#2}}%
  \pgfpointtaxicabnorm\pgf@xa%
  \ifdim\pgf@xa>.1pt\relax%
    \pgf@nfold@process@line@common{#1}{#2}{l}%
  \fi%
}%

\def\pgf@nfold@processcurveto#1#2#3#4#5#6{%
  \pgf@nfold@process@moveup%
  \pgf@offset@compute@tangents{\pgf@nfold@cur@last}{\pgf@x#1\pgf@y#2}{\pgf@x#3\pgf@y#4}{\pgf@x#5\pgf@y#6}%
  \let\pgf@nfold@next@type=c%
  \let\pgf@nfold@next@visible=1%
  \let\pgf@nfold@next@tang@i=\pgf@tmp@tang@i
  \let\pgf@nfold@next@tang@ii=\pgf@tmp@tang@ii
  \let\pgf@nfold@next@angle@i\pgf@tmp@angle@i
  \let\pgf@nfold@next@angle@ii\pgf@tmp@angle@ii
  \let\pgf@nfold@next@first\pgf@nfold@cur@last
  \def\pgf@nfold@next@supporta{\pgf@x#1\pgf@y#2}
  \def\pgf@nfold@next@supportb{\pgf@x#3\pgf@y#4}
  \def\pgf@nfold@next@last{\pgf@x#5\pgf@y#6}
  \pgf@nfold@process@segment%
}%

\def\pgf@nfold@processclosepath#1#2{%
  \pgf@process{\pgfpointdiff{\pgf@nfold@next@last}{\pgf@x#1\pgf@y#2}}%
  \pgfpointtaxicabnorm\pgf@xa%
  \ifdim\pgf@xa<0.1pt\relax%
    \pgf@nfold@process@closepath@zerolength{#1}{#2}%
  \else%
    \pgf@nfold@process@line@common{#1}{#2}{o}%
  \fi%
}%

\def\pgf@nfold@process@closepath@zerolength#1#2{%
  \pgf@nfold@process@moveup%
  \let\pgf@nfold@next@type=z%
  \let\pgf@nfold@next@visible=1%
  \let\pgf@nfold@next@tang@i\pgf@nfold@connsubpath@tang@i
  \let\pgf@nfold@next@tang@ii\pgf@nfold@connsubpath@tang@i
  \let\pgf@nfold@next@angle@i\pgf@nfold@connsubpath@angle@i
  \let\pgf@nfold@next@angle@ii\pgf@nfold@connsubpath@angle@i
  \let\pgf@nfold@next@first\pgf@nfold@cur@last
  \def\pgf@nfold@next@last{\pgf@x#1\pgf@y#2}%
  \pgf@nfold@process@segment%
}

\def\pgf@nfold@processinvisibleline#1#2{%
  \pgf@process{\pgfpointdiff{\pgf@nfold@next@last}{\pgf@x#1\pgf@y#2}}%
  \pgfpointtaxicabnorm\pgf@xa%
  \ifdim\pgf@xa>.1pt\relax%
    \pgf@nfold@process@line@common{#1}{#2}{i}%
  \else%
    \begingroup%
      \let\pgf@nfold@process@segment\relax%
      \pgf@nfold@sectolast@segment%
      \global\let\pgf@nfold@tmp\pgf@nfold@next@tang@ii%
    \endgroup%
    \pgfextract@process\pgf@nfold@next@first{\pgfpointdiff{\pgf@nfold@tmp}{\pgfqpoint{#1}{#2}}}%
    \pgf@nfold@tmp%
    \pgf@nfold@process@line@common{#1}{#2}{i}%
  \fi%
}%

\def\pgf@nfold@processlast{%
  \pgf@nfold@process@moveup%
  \let\pgf@nfold@next@type=t%
  \let\pgf@nfold@next@visible=0%
  \pgf@nfold@process@segment%
}

\def\pgf@nfold@addmacro#1{%
  \expandafter\expandafter\expandafter\gdef%
  \expandafter\expandafter\expandafter#1%
  \expandafter\expandafter\expandafter{%
  \expandafter#1\pgf@nfold@macrotoadd}%
}



\def\pgf@nfold@token@lineto#1#2#3{%
  \pgfoffsetline@{#1}{#2}{\pgf@nfold@shiftamount}{#3}%
}

\def\pgf@nfold@token@lineto@continue#1#2#3{%
  \pgfoffsetlinenomove@{#1}{#2}{\pgf@nfold@shiftamount}{#3}%
}

\def\pgf@nfold@token@invisibleline#1#2#3{%
  \pgfqpointscale{\pgf@nfold@shiftamount}{#3}%
  \pgf@xc=-\pgf@y%
  \pgf@y=\pgf@x%
  \pgf@x=\pgf@xc%
  \pgfpathmoveto{\pgfpointadd{}{#2}}%
}

\def\pgf@nfold@token@curveto#1#2#3#4#5#6{%
  \def\pgf@tmp@tang@i{#5}%
  \def\pgf@tmp@tang@ii{#6}%
  \pgf@offset@bezier@segment@{#1}{#2}{#3}{#4}{\pgf@nfold@shiftamount}%
  \pgfpathmoveto{\pgf@bezier@offset@i}%
  \pgfpathcurveto{\pgf@bezier@offset@ii}{\pgf@bezier@offset@iii}{\pgf@bezier@offset@iv}%
}

\def\pgf@nfold@token@curveto@continue#1#2#3#4#5#6{%
  \def\pgf@tmp@tang@i{#5}%
  \def\pgf@tmp@tang@ii{#6}%
  \pgf@offset@bezier@segment@{#1}{#2}{#3}{#4}{\pgf@nfold@shiftamount}%
  \pgfpathcurveto{\pgf@bezier@offset@ii}{\pgf@bezier@offset@iii}{\pgf@bezier@offset@iv}%
}

\def\pgf@nfold@token@closepath{%
  \pgfpathclose%
}

\def\pgf@nfold@token@edgecase@movetostart#1#2{%
  \pgfpathmoveto{\pgfpointadd%
    {#1}{\pgfpointpolar{#2+90}{\pgf@nfold@shiftamount}}}%
}


\def\pgf@nfold@parser@setupfirst{%
  \let\pgf@nfold@next@type=f%
  \let\pgf@nfold@next@visible=0%
  \edef\pgf@nfold@next@last{\pgf@x\the\pgf@path@lastx\pgf@y\the\pgf@path@lasty}%
}

\def\pgf@nfold@clampangle{
  \ifdim\pgfmathresult pt<-180pt\relax
    \pgfutil@tempdima=\pgfmathresult pt
    \advance\pgfutil@tempdima by 360pt
    \edef\pgfmathresult{\pgf@sys@tonumber\pgfutil@tempdima}
  \else\ifdim\pgfmathresult pt>180pt\relax
    \pgfutil@tempdima=\pgfmathresult pt
    \advance\pgfutil@tempdima by -360pt
    \edef\pgfmathresult{\pgf@sys@tonumber\pgfutil@tempdima}
  \fi\fi
}


\def\pgf@nfold@process@moveup{%
  \let\pgf@nfold@prev@type\pgf@nfold@cur@type%
  \let\pgf@nfold@prev@visible\pgf@nfold@cur@visible%
  \let\pgf@nfold@prev@tang@i\pgf@nfold@cur@tang@i%
  \let\pgf@nfold@prev@tang@ii\pgf@nfold@cur@tang@ii%
  \let\pgf@nfold@prev@angle@i\pgf@nfold@cur@angle@i%
  \let\pgf@nfold@prev@angle@ii\pgf@nfold@cur@angle@ii%
  \let\pgf@nfold@prev@first\pgf@nfold@cur@first%
  \let\pgf@nfold@prev@supporta\pgf@nfold@cur@supporta%
  \let\pgf@nfold@prev@supportb\pgf@nfold@cur@supportb%
  \let\pgf@nfold@prev@last\pgf@nfold@cur@last%
  \let\pgf@nfold@cur@type\pgf@nfold@next@type%
  \let\pgf@nfold@cur@visible\pgf@nfold@next@visible%
  \let\pgf@nfold@cur@tang@i\pgf@nfold@next@tang@i%
  \let\pgf@nfold@cur@tang@ii\pgf@nfold@next@tang@ii%
  \let\pgf@nfold@cur@angle@i\pgf@nfold@next@angle@i%
  \let\pgf@nfold@cur@angle@ii\pgf@nfold@next@angle@ii
  \let\pgf@nfold@cur@first\pgf@nfold@next@first%
  \let\pgf@nfold@cur@supporta\pgf@nfold@next@supporta%
  \let\pgf@nfold@cur@supportb\pgf@nfold@next@supportb%
  \let\pgf@nfold@cur@last\pgf@nfold@next@last%
}


\def\pgfoffsetpath#1#2{%
  \begingroup
    \pgfmathsetlengthmacro\pgf@nfold@parsed@hwidth{#2}
    \pgf@x=\pgf@nfold@parsed@hwidth\relax
    \ifdim\pgf@x<0pt\relax
      \pgf@x=-\pgf@x
      \def\pgf@nfold@shift@fraction{-1}
    \else
      \def\pgf@nfold@shift@fraction{1}
    \fi
    \edef\pgf@nfold@parsed@hwidth{\the\pgf@x}
    \pgfoffsetpathqfraction{#1}{\pgf@nfold@parsed@hwidth}{\pgf@nfold@shift@fraction}
  \endgroup
}

\def\pgfoffsetpathfraction#1#2#3{%
  \begingroup
    \pgfmathsetlengthmacro\pgf@nfold@parsed@hwidth{#2}
    \pgfmathsetmacro\pgf@nfold@shift@fraction{#3}
    \pgfoffsetpathqfraction{#1}{\pgf@nfold@parsed@hwidth}{\pgf@nfold@shift@fraction}
  \endgroup
}

\def\pgfoffsetpathindex#1#2#3#4{%
  \begingroup
    \pgfmathsetmacro\pgf@nfold@shift@fraction{-1+2*(#3-1)/(#4-1)}
    \pgfoffsetpathqfraction{#1}{#2}{\pgf@nfold@shift@fraction}
  \endgroup
}

\def\pgfoffsetpathqfraction#1#2#3{%
  \begingroup
    \pgf@x=#2\relax
    \edef\pgf@nfold@hwidth{\the\pgf@x}
    \edef\pgf@nfold@shift@fraction{#3}
    \pgf@x=\pgf@nfold@shift@fraction\pgf@x\relax
    \edef\pgf@nfold@shiftamount{\pgf@sys@tonumber\pgf@x}
    \pgfprocessround{#1}{\pgf@nfold@cachedpath}%
    \pgf@nfold@parsesoftpath{\pgf@nfold@cachedpath}{\parsedpath}%
    \pgftransformreset%
    \parsedpath%
  \endgroup
}

\endinput
