| 1 |
% Copyright 2019 by Till Tantau |
| 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 |
\ProvidesFileRCS{pgfmodulesnakes.code.tex} |
| 11 |
|
| 12 |
\pgfwarning{Snakes have been superseded by decorations. Use the module decorations instead of snakes} |
| 13 |
|
| 14 |
\usepgfmodule{decorations}% |
| 15 |
|
| 16 |
\endinput |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
\newdimen\pgfsnakeremainingdistance |
| 21 |
\newdimen\pgfsnakecompleteddistance |
| 22 |
|
| 23 |
|
| 24 |
% Creates a new pgf snake |
| 25 |
% |
| 26 |
% #1 = snake name |
| 27 |
% #1 = initial state |
| 28 |
% #3 = states of the snake |
| 29 |
% |
| 30 |
% |
| 31 |
% This command declares a new snake for later use. The second |
| 32 |
% parameter specifies the states of the snake, see also the |
| 33 |
% description of pgfpathsnake. |
| 34 |
% |
| 35 |
% Inside the code of #3 the command \state may be used. This command |
| 36 |
% will only be defined while #3 is executed. |
| 37 |
% |
| 38 |
% Example: |
| 39 |
% |
| 40 |
% \pgfdeclaresnake{zig zag}{one zig zag} |
| 41 |
% { |
| 42 |
% \state{one zig zag}[width=10pt] |
| 43 |
% { |
| 44 |
% \pgfpathlineto{\pgfpoint{2.5pt}{2.5pt}} |
| 45 |
% \pgfpathlineto{\pgfpoint{7.5pt}{-2.5pt}} |
| 46 |
% \pgfpathlineto{\pgfpoint{10pt}{0pt}} |
| 47 |
% } |
| 48 |
% \state{final} |
| 49 |
% { |
| 50 |
% \pgfpathlineto{\pgfpoint{\pgfsnakeremainingdistance}{0pt}} |
| 51 |
% } |
| 52 |
% } |
| 53 |
|
| 54 |
\long\def\pgfdeclaresnake#1#2#3{% |
| 55 |
\def\pgf@snake@name{#1}% |
| 56 |
\pgfutil@namedef{pgf@snake@@#1@initial}{#2}% |
| 57 |
\let\pgf@orig@state=\state% |
| 58 |
\let\state=\pgf@snake@state |
| 59 |
#3 |
| 60 |
\let\state=\pgf@orig@state% |
| 61 |
}% |
| 62 |
|
| 63 |
|
| 64 |
% Sets the additional transformation applied to every segment of a snake |
| 65 |
% |
| 66 |
% #1 = transformation |
| 67 |
% |
| 68 |
% Example: |
| 69 |
% |
| 70 |
% \pgfsetsnakesegmenttransformation{\pgftransformyshift{5pt}} |
| 71 |
|
| 72 |
\def\pgfsetsnakesegmenttransformation#1{\def\pgf@snakeadditionaltransform{#1}}% |
| 73 |
\pgfsetsnakesegmenttransformation{}% |
| 74 |
|
| 75 |
|
| 76 |
% Declares a new state |
| 77 |
% |
| 78 |
% #1 = state name |
| 79 |
% #2 = options |
| 80 |
% #3 = path element |
| 81 |
% |
| 82 |
% Description: |
| 83 |
% |
| 84 |
% When a snake is drawn and the current state is #1, the following |
| 85 |
% happens. First, the options are executed, which will possible change |
| 86 |
% the state. If that does not happen, the path element is added to the |
| 87 |
% path and the coordinate system is translated by the path element's |
| 88 |
% width (which is specified using the width option). |
| 89 |
% |
| 90 |
% Example: |
| 91 |
% |
| 92 |
% \state{initial}[width=10pt] |
| 93 |
% { |
| 94 |
% \pgfpathlineto{\pgfpoint{2.5pt}{2.5pt}} |
| 95 |
% \pgfpathlineto{\pgfpoint{7.5pt}{-2.5pt}} |
| 96 |
% \pgfpathlineto{\pgfpoint{10pt}{0pt}} |
| 97 |
% } |
| 98 |
|
| 99 |
\def\pgf@snake@state#1{\pgfutil@ifnextchar[{\pgf@@snake@start#1}{\pgf@@snake@start#1[]}}%}% |
| 100 |
\def\pgf@@snake@start#1[#2]#3{% |
| 101 |
\pgfutil@namedef{pgf@snake@@\pgf@snake@name @#1@options}{#2}% |
| 102 |
\pgfutil@namedef{pgf@snake@@\pgf@snake@name @#1@code}{#3}% |
| 103 |
}% |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
% Use multiple snakes |
| 108 |
% |
| 109 |
% #1 = list of snake names/length pairs |
| 110 |
% #2 = point to which the snake leads |
| 111 |
% |
| 112 |
% This operation uses the snakes in parameter #1 to get to the point |
| 113 |
% #4. The parameter #1 should contain pairs consisting of a snake name |
| 114 |
% and a length for which this snake should be used. When the length is |
| 115 |
% computed, the dimensions \pgfsnakeremainingdistance and |
| 116 |
% \pgfsnakecompleteddistance will have been set to the length of the |
| 117 |
% total distance to #2 that has already been covered/that still needs |
| 118 |
% to be covered. |
| 119 |
% |
| 120 |
% Example: |
| 121 |
% |
| 122 |
% \pgfpathsnakesto{{lineto}{1cm},{zig zag}{\pgfsnakeremainingdistance}}{\pgfpoint{2cm}{3cm}} |
| 123 |
|
| 124 |
\def\pgfpathsnakesto#1#2{% |
| 125 |
\begingroup% |
| 126 |
% compute target vector |
| 127 |
\pgfpointtransformed{#2}% |
| 128 |
\advance\pgf@x by-\pgf@path@lastx% |
| 129 |
\advance\pgf@y by-\pgf@path@lasty% |
| 130 |
% |
| 131 |
% MW: Calculate the angle of the snake. |
| 132 |
% |
| 133 |
\edef\pgf@marshal{% |
| 134 |
\noexpand\pgfmathanglebetweenpoints{\noexpand\pgfpointorigin}{% |
| 135 |
\noexpand\pgf@x\the\pgf@x\noexpand\pgf@y\the\pgf@y}% |
| 136 |
}% |
| 137 |
\pgf@marshal% |
| 138 |
\let\pgfsnakeangle\pgfmathresult% |
| 139 |
% |
| 140 |
% MW: Use this instead for length (?). More accurate and only a few operations slower. |
| 141 |
% |
| 142 |
\pgfmathveclen@{\pgfmath@tonumber{\pgf@x}}{\pgfmath@tonumber{\pgf@y}}% |
| 143 |
\pgfsnakeremainingdistance\pgfmathresult pt\relax% |
| 144 |
% |
| 145 |
% Ok, now normalize the vector... |
| 146 |
\pgf@xa=\pgf@x% |
| 147 |
\pgf@ya=\pgf@y% |
| 148 |
\pgf@process{\pgfpointnormalised{}}% |
| 149 |
% ok, now computer length (arghh...) |
| 150 |
% In theory that's easy: divide the larger of the values x or y by |
| 151 |
% the normalized x or y. Well... |
| 152 |
% |
| 153 |
% First, make xa and ya positive: |
| 154 |
% \ifdim\pgf@xa<0pt% |
| 155 |
% \pgf@xa=-\pgf@xa% |
| 156 |
% \fi% |
| 157 |
% \ifdim\pgf@ya<0pt% |
| 158 |
% \pgf@ya=-\pgf@ya% |
| 159 |
% \fi% |
| 160 |
% % Now do division: |
| 161 |
% \ifdim\pgf@xa>\pgf@ya% |
| 162 |
% \c@pgf@counta=\pgf@x% |
| 163 |
% \ifnum\c@pgf@counta=0\relax% |
| 164 |
% \else% |
| 165 |
% \divide\c@pgf@counta by 255\relax% |
| 166 |
% \pgf@xa=16\pgf@xa\relax% |
| 167 |
% \divide\pgf@xa by\c@pgf@counta% |
| 168 |
% \pgf@xa=16\pgf@xa\relax% |
| 169 |
% \fi% |
| 170 |
% \else% |
| 171 |
% \c@pgf@counta=\pgf@y% |
| 172 |
% \ifnum\c@pgf@counta=0\relax% |
| 173 |
% \else% |
| 174 |
% \divide\c@pgf@counta by 255\relax% |
| 175 |
% \pgf@ya=16\pgf@ya\relax% |
| 176 |
% \divide\pgf@ya by\c@pgf@counta% |
| 177 |
% \pgf@xa=16\pgf@ya\relax% |
| 178 |
% \fi% |
| 179 |
% \fi% |
| 180 |
% % Make positive: |
| 181 |
% \ifdim\pgf@xa<0pt% |
| 182 |
% \pgf@xa=-\pgf@xa% |
| 183 |
% \fi% |
| 184 |
% % Ok, now we draw things... |
| 185 |
\edef\pgf@list{#1}% |
| 186 |
% \pgfsnakeremainingdistance=\pgf@xa% |
| 187 |
\pgfsnakecompleteddistance=0pt% |
| 188 |
\pgf@xb=\pgf@x% |
| 189 |
\pgf@yb=\pgf@y% |
| 190 |
\pgfutil@for\pgf@temp:=\pgf@list\do{% |
| 191 |
\ifx\pgf@temp\pgfutil@empty% |
| 192 |
\else% |
| 193 |
\expandafter\pgf@snake@invoke\pgf@temp% |
| 194 |
\fi% |
| 195 |
}% |
| 196 |
\endgroup% |
| 197 |
}% |
| 198 |
|
| 199 |
\def\pgf@snake@invoke#1#2{% |
| 200 |
\pgfmathsetlength\pgf@xa{#2}% |
| 201 |
{% |
| 202 |
\edef\pgf@marshal{\noexpand\pgfpathsnakealongvector{#1}{\the\pgf@xa}{\noexpand\pgfqpoint{\the\pgf@xb}{\the\pgf@yb}}}% |
| 203 |
\pgf@marshal% |
| 204 |
}% |
| 205 |
\advance\pgfsnakecompleteddistance by\pgf@xa% |
| 206 |
\advance\pgfsnakeremainingdistance by-\pgf@xa% |
| 207 |
}% |
| 208 |
|
| 209 |
% Use a snake |
| 210 |
% |
| 211 |
% #1 = snake name |
| 212 |
% #2 = point to which the snake leads |
| 213 |
% |
| 214 |
% This operation mainly computes \pgfpathsnakealongvector for a vector |
| 215 |
% appropriately chosen. See \pgfpathsnakealongvector for details. |
| 216 |
% |
| 217 |
% Example: |
| 218 |
% |
| 219 |
% \pgfpathsnaketo{zig zag}{\pgfpoint{2cm}{3cm}} |
| 220 |
|
| 221 |
\def\pgfpathsnaketo#1#2{\pgfpathsnakesto{{#1}{\pgfsnakeremainingdistance}}{#2}}% |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
% Use a snake |
| 226 |
% |
| 227 |
% #1 = snake name |
| 228 |
% #2 = length of the snake |
| 229 |
% #3 = vector along which the snake grows, should have unit length. |
| 230 |
% |
| 231 |
% This command draws a snake (more precisely, it adds a snake to the |
| 232 |
% path). This works as follows: |
| 233 |
% |
| 234 |
% First, the coordinate system is transformed such that the vector #3 |
| 235 |
% points to the right. |
| 236 |
% |
| 237 |
% Next, the state `initial' of the snake is entered. Unless the |
| 238 |
% options of this state cause it to switch to another state, the path |
| 239 |
% element is added to the path. Then, the coordinate system is |
| 240 |
% translated by the width of the path element as specified in the |
| 241 |
% width option of the path element. The dimensions |
| 242 |
% \pgfsnakeremainingdistance and \pgfsnakecompleteddistance are |
| 243 |
% updated. |
| 244 |
% |
| 245 |
% The process ends when the state `final' is entered. The code of the |
| 246 |
% final state is executed and the process stops. |
| 247 |
% |
| 248 |
% Example: |
| 249 |
% |
| 250 |
% \pgfpathsnakealongvector{zig zag}{100pt}{\pgfpolar{30}{1pt}} |
| 251 |
|
| 252 |
\def\pgfpathsnakealongvector#1#2#3{% |
| 253 |
\pgfutil@ifundefined{pgf@snake@@#1@initial}{\pgferror{Undefined snake ``#1''}} |
| 254 |
{ |
| 255 |
\begingroup% keep things local |
| 256 |
\pgftransformreset% |
| 257 |
\pgf@pt@x=\pgf@path@lastx% evil trickery to transform to the last point |
| 258 |
\pgf@pt@y=\pgf@path@lasty% |
| 259 |
\pgf@process{#3}% |
| 260 |
\pgf@xa=\pgf@x% |
| 261 |
\pgf@ya=\pgf@y% |
| 262 |
\pgf@xb=-\pgf@y% |
| 263 |
\pgf@yb=\pgf@x% |
| 264 |
\pgftransformcm |
| 265 |
{\pgf@sys@tonumber{\pgf@xa}}{\pgf@sys@tonumber{\pgf@ya}} |
| 266 |
{\pgf@sys@tonumber{\pgf@xb}}{\pgf@sys@tonumber{\pgf@yb}} |
| 267 |
{\pgfpointorigin}% |
| 268 |
% Now, setup the automaton |
| 269 |
\expandafter\let\expandafter\pgf@snake@current@state\expandafter=\csname pgf@snake@@#1@initial\endcsname% |
| 270 |
\def\pgf@snake@name{#1}% |
| 271 |
\pgfsnakecompleteddistance=0pt% |
| 272 |
\pgfmathsetlength\pgfsnakeremainingdistance{#2}% |
| 273 |
\pgf@snake@run% |
| 274 |
% Last step: |
| 275 |
{% |
| 276 |
\pgftransformxshift{\pgfsnakecompleteddistance}% |
| 277 |
\pgf@snakeadditionaltransform% |
| 278 |
\csname pgf@snake@@#1@final@code\endcsname% |
| 279 |
}% |
| 280 |
\endgroup% |
| 281 |
}% |
| 282 |
}% |
| 283 |
|
| 284 |
\def\pgf@final@text{final}% |
| 285 |
|
| 286 |
\def\pgf@snake@run{% |
| 287 |
\let\pgf@snake@next=\pgf@snake@do@state% |
| 288 |
\ifx\pgf@snake@current@state\pgf@final@text% |
| 289 |
\let\pgf@snake@next=\relax% |
| 290 |
\fi% |
| 291 |
\pgf@snake@next% |
| 292 |
}% |
| 293 |
|
| 294 |
\def\pgf@snake@do@state{% |
| 295 |
\let\pgf@snake@next=\relax% |
| 296 |
\let\pgf@snake@next@state=\pgf@snake@current@state% |
| 297 |
% execute options |
| 298 |
\expandafter\expandafter\expandafter\pgf@snakes@setter |
| 299 |
\expandafter\expandafter\expandafter{\csname pgf@snake@@\pgf@snake@name @\pgf@snake@current@state @options\endcsname}% |
| 300 |
\ifx\pgf@snake@next\relax% |
| 301 |
\let\pgf@snake@next=\pgf@snake@do@code% |
| 302 |
\fi% |
| 303 |
\pgf@snake@next% |
| 304 |
}% |
| 305 |
\def\pgf@snakes@setter{% |
| 306 |
\pgfqkeys{/pgf/snakes}% |
| 307 |
}% |
| 308 |
|
| 309 |
\def\pgf@snake@do@code{% |
| 310 |
% Ok, execute code: |
| 311 |
{% |
| 312 |
\pgftransformxshift{\pgfsnakecompleteddistance}% |
| 313 |
\pgf@snakeadditionaltransform% |
| 314 |
\csname pgf@snake@@\pgf@snake@name @\pgf@snake@current@state @code\endcsname% |
| 315 |
}% |
| 316 |
% next, do transformation and update |
| 317 |
\pgfmathsetlength{\pgf@xa}{\pgf@snake@width}% |
| 318 |
\advance\pgfsnakeremainingdistance by-\pgf@xa% |
| 319 |
\advance\pgfsnakecompleteddistance by\pgf@xa% |
| 320 |
% Next iteration: |
| 321 |
\let\pgf@snake@current@state=\pgf@snake@next@state% |
| 322 |
\pgf@snake@run% |
| 323 |
}% |
| 324 |
|
| 325 |
\pgfkeys{ |
| 326 |
/pgf/snakes/width/.code=\def\pgf@snake@width{#1}\pgf@snake@switch@if#1 to final\pgf@stop,% |
| 327 |
/pgf/snakes/switch if less than/.code=\pgf@snake@switch@if#1\pgf@stop,% |
| 328 |
/pgf/snakes/next state/.store in=\pgf@snake@next@state% |
| 329 |
}% |
| 330 |
|
| 331 |
\def\pgf@snake@switch@if#1to #2\pgf@stop{% |
| 332 |
\ifx\pgf@snake@next\relax% |
| 333 |
\pgfmathsetlength\pgf@x{#1}% |
| 334 |
\ifdim\pgfsnakeremainingdistance<\pgf@x% |
| 335 |
\def\pgf@snake@current@state{#2}% |
| 336 |
\let\pgf@snake@next=\pgf@snake@run% |
| 337 |
\fi% |
| 338 |
\fi% |
| 339 |
}% |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
% lineto snake |
| 344 |
% |
| 345 |
% This snake simply adds a straight line. This snake is mainly useful |
| 346 |
% in conjunction with other snakes. |
| 347 |
|
| 348 |
\pgfdeclaresnake{lineto}{final} |
| 349 |
{% |
| 350 |
\state{final} |
| 351 |
{ \pgfpathlineto{\pgfqpoint{\pgfsnakeremainingdistance}{0pt}} } |
| 352 |
}% |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
% moveto snake |
| 357 |
% |
| 358 |
% This snake simply jumps to the end. This snake, too, is mainly |
| 359 |
% useful in conjunction with other snakes. |
| 360 |
|
| 361 |
\pgfdeclaresnake{moveto}{final} |
| 362 |
{% |
| 363 |
\state{final} |
| 364 |
{ \pgfpathmoveto{\pgfqpoint{\pgfsnakeremainingdistance}{0pt}} } |
| 365 |
}% |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
\endinput |