| 1 |
% Copyright 2018 by Mark Wibrow |
| 2 |
% |
| 3 |
% This file may be distributed and/or modified |
| 4 |
% |
| 5 |
% 1. under the LaTeX Project Public License and/or |
| 6 |
% 2. under the GNU Public License. |
| 7 |
% |
| 8 |
% See the file doc/generic/pgf/licenses/LICENSE for more details. |
| 9 |
|
| 10 |
\newdimen\pgflsystemstep |
| 11 |
\newcount\c@pgf@lsystem@iteration |
| 12 |
|
| 13 |
\newif\ifpgf@lsystem@randomize@step |
| 14 |
\newif\ifpgf@lsystem@randomize@angle |
| 15 |
|
| 16 |
\pgfkeys{/pgf/lindenmayer system/.cd,% |
| 17 |
step/.code={\pgfmathsetlength\pgflsystemstep{#1}},% |
| 18 |
randomize step percent/.code={% |
| 19 |
\pgfmathparse{#1}% |
| 20 |
\let\pgflsystemrandomizesteppercent=\pgfmathresult% |
| 21 |
\ifdim\pgfmathresult pt=0pt\relax% |
| 22 |
\pgf@lsystem@randomize@stepfalse% |
| 23 |
\else% |
| 24 |
\pgf@lsystem@randomize@steptrue% |
| 25 |
\fi% |
| 26 |
},% |
| 27 |
left angle/.code={\pgfmathparse{#1}\let\pgflsystemleftangle=\pgfmathresult},% |
| 28 |
right angle/.code={\pgfmathparse{#1}\let\pgflsystemrightangle=\pgfmathresult},% |
| 29 |
angle/.style={/pgf/lindenmayer system/left angle={#1}, /pgf/lindenmayer system/right angle={#1}},% |
| 30 |
randomize angle percent/.code={% |
| 31 |
\pgfmathparse{#1}% |
| 32 |
\let\pgflsystemrandomizeanglepercent=\pgfmathresult% |
| 33 |
\ifdim\pgfmathresult pt=0pt\relax% |
| 34 |
\pgf@lsystem@randomize@anglefalse% |
| 35 |
\else% |
| 36 |
\pgf@lsystem@randomize@angletrue% |
| 37 |
\fi% |
| 38 |
}% |
| 39 |
}% |
| 40 |
|
| 41 |
\pgfkeys{/pgf/lindenmayer system/.cd, |
| 42 |
step=5pt,% |
| 43 |
randomize step percent=0,% |
| 44 |
angle=90,% |
| 45 |
randomize angle percent=0% |
| 46 |
}% |
| 47 |
|
| 48 |
\long\def\pgfdeclarelindenmayersystem#1#2{% |
| 49 |
\pgfutil@ifundefined{pgf@lsystem@#1}% |
| 50 |
{% |
| 51 |
\begingroup% |
| 52 |
\edef\pgf@lsystem@name{#1}% |
| 53 |
\expandafter\global\expandafter\let\csname pgf@lsystem@#1\endcsname=\pgf@lsystem@name% |
| 54 |
\let\symbol=\pgf@lsystem@symbol% |
| 55 |
\let\rule=\pgf@lsystem@rule% |
| 56 |
#2% |
| 57 |
\endgroup% |
| 58 |
}% |
| 59 |
{\pgferror{Lindenmayer system `#1' is already defined}}% |
| 60 |
}% |
| 61 |
|
| 62 |
\def\pgf@lsystem@symbol#1#2{% |
| 63 |
\expandafter\gdef\csname pgf@lsystem@\pgf@lsystem@name @symbol@#1\endcsname{#2}% |
| 64 |
}% |
| 65 |
|
| 66 |
\def\pgf@lsystem@rule#1{\expandafter\pgf@lsystem@rule@#1\pgf@stop}% |
| 67 |
\def\pgf@lsystem@rule@#1{\def\pgf@lsystem@rule@head{#1}\pgf@lsystem@rule@@}% |
| 68 |
\def\pgf@lsystem@rule@@#1->{% Now some `fooling around' to deal with unwanted spaces. |
| 69 |
\let\pgf@lsystem@rule@body=\pgfutil@empty% |
| 70 |
\pgfutil@ifnextchar x{\pgf@lsystem@rule@@@}{\pgf@lsystem@rule@@@}}% |
| 71 |
\def\pgf@lsystem@rule@@@#1{% |
| 72 |
\ifx#1\pgf@stop% |
| 73 |
\expandafter\global\expandafter\let% |
| 74 |
\csname pgf@lsystem@\pgf@lsystem@name @rule@\pgf@lsystem@rule@head\endcsname=\pgf@lsystem@rule@body% |
| 75 |
\else% |
| 76 |
\edef\pgf@lsystem@rule@body{\pgf@lsystem@rule@body#1}% |
| 77 |
\expandafter\pgf@lsystem@rule@@@% |
| 78 |
\fi% |
| 79 |
}% |
| 80 |
|
| 81 |
\def\pgflindenmayersystem#1#2#3{% |
| 82 |
\begingroup% |
| 83 |
\edef\pgf@lsystem@name{#1}% |
| 84 |
\edef\pgf@lsystem@axiom{#2}% |
| 85 |
\pgfmathtruncatemacro\pgf@lsystem@order{#3}% |
| 86 |
% |
| 87 |
\let\pgf@lsystem@current@symbol=\relax% |
| 88 |
% |
| 89 |
\c@pgf@lsystem@iteration=0\relax% |
| 90 |
% |
| 91 |
\ifnum\pgf@lsystem@order=0\relax% |
| 92 |
\expandafter\pgf@lsystem@draw\pgf@lsystem@axiom\pgf@stop |
| 93 |
\let\pgf@lsystem@next=\pgf@lsystem@end% |
| 94 |
\else% |
| 95 |
\let\pgf@lsystem@next=\pgf@lsystem@run% |
| 96 |
\fi% |
| 97 |
\expandafter\pgf@lsystem@next\pgf@lsystem@axiom\pgf@lsystem@stop% |
| 98 |
}% |
| 99 |
|
| 100 |
\def\pgf@lsystem@run#1{% |
| 101 |
\ifx#1\pgf@lsystem@stop% |
| 102 |
\def\pgf@lsystem@token{\pgf@lsystem@stop}% |
| 103 |
\let\pgf@lsystem@next=\pgf@lsystem@end% |
| 104 |
\else% |
| 105 |
\ifx#1\pgf@stop% |
| 106 |
\advance\c@pgf@lsystem@iteration by-1\relax% |
| 107 |
\let\pgf@system@token=\pgfutil@empty% |
| 108 |
\let\pgf@lsystem@next=\pgf@lsystem@run% |
| 109 |
\else% |
| 110 |
% Does #1 appear on the RHS of a rule...? |
| 111 |
\expandafter\let\expandafter\pgf@lsystem@token\expandafter=% |
| 112 |
\csname pgf@lsystem@\pgf@lsystem@name @rule@#1\endcsname% |
| 113 |
\ifx\pgf@lsystem@token\relax% |
| 114 |
% ...nope. So draw it straight away. |
| 115 |
\pgf@lsystem@draw#1\pgf@stop% |
| 116 |
\let\pgf@lsystem@token=\pgfutil@empty% |
| 117 |
\else% |
| 118 |
% ...yep. So, if the order has been reached draw the LHS |
| 119 |
% immediately. Otherwise add the LHS to the token stream |
| 120 |
% and continue. |
| 121 |
\advance\c@pgf@lsystem@iteration by1\relax% |
| 122 |
\ifnum\c@pgf@lsystem@iteration=\pgf@lsystem@order% |
| 123 |
\expandafter\pgf@lsystem@draw\pgf@lsystem@token \pgf@stop% |
| 124 |
\advance\c@pgf@lsystem@iteration by-1\relax% |
| 125 |
\let\pgf@lsystem@token=\pgfutil@empty% |
| 126 |
\else% |
| 127 |
\expandafter\def\expandafter\pgf@lsystem@token\expandafter{\pgf@lsystem@token \pgf@stop}% |
| 128 |
\fi% |
| 129 |
\fi% |
| 130 |
\let\pgf@lsystem@next=\pgf@lsystem@run% |
| 131 |
\fi% |
| 132 |
\fi% |
| 133 |
\expandafter\pgf@lsystem@next\pgf@lsystem@token}% |
| 134 |
|
| 135 |
\def\pgf@lsystem@end#1\pgf@lsystem@stop{\endgroup}% |
| 136 |
|
| 137 |
\def\pgf@lsystem@draw#1{% |
| 138 |
\ifx#1\pgf@stop% |
| 139 |
\let\pgf@lsystem@next=\relax% |
| 140 |
\else% |
| 141 |
\expandafter\let\expandafter\pgf@lsystem@current@symbol\expandafter=% |
| 142 |
\csname pgf@lsystem@\pgf@lsystem@name @symbol@#1\endcsname% |
| 143 |
\ifx\pgf@lsystem@current@symbol\relax% Try a default symbol. |
| 144 |
\expandafter\let\expandafter\pgf@lsystem@current@symbol\expandafter=% |
| 145 |
\csname pgf@lsystem@symbol@default@#1\endcsname% |
| 146 |
\fi% |
| 147 |
\let\pgf@lsystem@next=\pgf@lsystem@@draw% |
| 148 |
\fi% |
| 149 |
\pgf@lsystem@next}% |
| 150 |
|
| 151 |
\def\pgf@lsystem@@draw{% |
| 152 |
\edef\pgflsystemcurrentstep{\the\pgflsystemstep}% |
| 153 |
\let\pgflsystemcurrentrightangle=\pgflsystemrightangle% |
| 154 |
\let\pgflsystemcurrentrightangle=\pgflsystemleftangle% |
| 155 |
\pgf@lsystem@current@symbol% |
| 156 |
\pgf@lsystem@draw}% |
| 157 |
|
| 158 |
\expandafter\def\csname |
| 159 |
pgf@lsystem@symbol@default@F\endcsname{\pgflsystemdrawforward}% |
| 160 |
\expandafter\def\csname |
| 161 |
pgf@lsystem@symbol@default@f\endcsname{\pgflsystemmoveforward}% |
| 162 |
\expandafter\def\csname |
| 163 |
pgf@lsystem@symbol@default@+\endcsname{\pgflsystemturnleft}% |
| 164 |
\expandafter\def\csname |
| 165 |
pgf@lsystem@symbol@default@-\endcsname{\pgflsystemturnright}% |
| 166 |
\expandafter\def\csname |
| 167 |
pgf@lsystem@symbol@default@[\endcsname{\pgflsystemsavestate}% |
| 168 |
\expandafter\def\csname |
| 169 |
pgf@lsystem@symbol@default@]\endcsname{\pgflsystemrestorestate}% |
| 170 |
|
| 171 |
\def\pgflsystemradonmizestep{% |
| 172 |
\ifpgf@lsystem@randomize@step% |
| 173 |
\pgfmathrand% |
| 174 |
\pgf@x=\pgflsystemrandomizesteppercent pt\relax% |
| 175 |
\pgf@x=\pgfmathresult\pgf@x% |
| 176 |
\divide\pgf@x by20\relax% |
| 177 |
\advance\pgf@x by\pgflsystemstep\relax% |
| 178 |
\edef\pgflsystemcurrentstep{\the\pgf@x}% |
| 179 |
\else% |
| 180 |
\edef\pgflsystemcurrentstep{\the\pgflsystemstep}% |
| 181 |
\fi% |
| 182 |
}% |
| 183 |
|
| 184 |
\def\pgflsystemdrawforward{% |
| 185 |
\pgflsystemradonmizestep |
| 186 |
\pgftransformxshift{+\pgflsystemcurrentstep}% |
| 187 |
\pgfpathlineto{\pgfpointorigin}}% |
| 188 |
|
| 189 |
\def\pgflsystemmoveforward{% |
| 190 |
\pgflsystemradonmizestep |
| 191 |
\pgftransformxshift{+\pgflsystemcurrentstep}% |
| 192 |
\pgfpathmoveto{\pgfpointorigin}}% |
| 193 |
|
| 194 |
\def\pgflsystemranomizerightangle{% |
| 195 |
\ifpgf@lsystem@randomize@angle% |
| 196 |
\pgf@x=\pgflsystemrandomizeanglepercent pt\relax% |
| 197 |
\divide\pgf@x by20\relax% |
| 198 |
\pgfmathrand% |
| 199 |
\pgf@x=\pgfmathresult\pgf@x% |
| 200 |
\advance\pgf@x by\pgflsystemrightangle pt\relax% |
| 201 |
\edef\pgflsystemcurrentrightangle{\pgfmath@tonumber{\pgf@x}}% |
| 202 |
\else% |
| 203 |
\let\pgflsystemcurrentrightangle=\pgflsystemrightangle% |
| 204 |
\fi% |
| 205 |
}% |
| 206 |
|
| 207 |
\def\pgflsystemranomizeleftangle{% |
| 208 |
\ifpgf@lsystem@randomize@angle% |
| 209 |
\pgf@x=\pgflsystemrandomizeanglepercent pt\relax% |
| 210 |
\divide\pgf@x by20\relax% |
| 211 |
\pgfmathrand% |
| 212 |
\pgf@x=\pgfmathresult\pgf@x% |
| 213 |
\advance\pgf@x by\pgflsystemleftangle pt\relax% |
| 214 |
\edef\pgflsystemcurrentleftangle{\pgfmath@tonumber{\pgf@x}}% |
| 215 |
\else% |
| 216 |
\let\pgflsystemcurrentleftangle=\pgflsystemleftangle% |
| 217 |
\fi% |
| 218 |
}% |
| 219 |
|
| 220 |
\def\pgflsystemturnright{% |
| 221 |
\pgflsystemranomizerightangle |
| 222 |
\pgftransformrotate{-\pgflsystemcurrentrightangle}}% |
| 223 |
|
| 224 |
\def\pgflsystemturnleft{% |
| 225 |
\pgflsystemranomizeleftangle |
| 226 |
\pgftransformrotate{\pgflsystemcurrentleftangle}}% |
| 227 |
|
| 228 |
\def\pgflsystemsavestate{\begingroup}% |
| 229 |
\def\pgflsystemrestorestate{\endgroup\pgfpathmoveto{\pgfpointorigin}}% |