| 1 |
% Copyright 2018 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 |
\RequirePackage{pgfrcs} |
| 11 |
|
| 12 |
\ProvidesPackageRCS{pgfautomata.sty} |
| 13 |
|
| 14 |
|
| 15 |
\RequirePackage{pgf} |
| 16 |
|
| 17 |
\PackageWarning{pgfautomata}{This package is obsolete.} |
| 18 |
|
| 19 |
\ProcessOptions |
| 20 |
|
| 21 |
% |
| 22 |
% Package for drawing finite automata. |
| 23 |
% |
| 24 |
|
| 25 |
|
| 26 |
\newdimen\pgf@stateradius |
| 27 |
\newdimen\pgf@mooreradius |
| 28 |
\newdimen\pgf@doublethickness |
| 29 |
\newdimen\pgf@statebackoff |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
% Automaton environment - setup of certain drawing parameters like arrows. |
| 35 |
% |
| 36 |
% Example: |
| 37 |
% |
| 38 |
% \begin{pgfpicture}{-2cm}{-2cm}{2cm}{2cm}% |
| 39 |
% \begin{pgfautomaton} |
| 40 |
% \pgfstaterejecting{q1}{\pgforigin}{$q_1$} |
| 41 |
% \pgfstateaccepting{q2}{\pgfstaterelative[east]{q1}{1cm}}{$q_2$} |
| 42 |
% \pgfstateaccepting{q3}{\pgfstaterelative[east]{q2}{1cm}}{$q_3$} |
| 43 |
% |
| 44 |
% \pgfstateconnect{q1}{q2}[.5]{$0$} |
| 45 |
% \pgfstateconnect{q2}{q3}[.5]{$1$} |
| 46 |
% \pgfstateloop{q1}{90}{$1$} |
| 47 |
% \pgfstateloop{q2}{90}{$1$} |
| 48 |
% \end{pgfautomaton} |
| 49 |
% \end{pgfpicture} |
| 50 |
|
| 51 |
\def\pgfautomaton{\begin{pgfscope}% |
| 52 |
\pgfsetendarrow{\pgfarrowto}% |
| 53 |
\pgfnodesetsepend{4pt}\ignorespaces} |
| 54 |
\def\endpgfautomaton{\end{pgfscope}} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
% Set the outer state radius to #1. |
| 59 |
% |
| 60 |
% #1 = new state radius. |
| 61 |
% |
| 62 |
% Example: |
| 63 |
% |
| 64 |
% \pgfsetstateradius{10pt} |
| 65 |
|
| 66 |
\def\pgfsetstateradius#1{\setlength\pgf@stateradius{#1}\ignorespaces} |
| 67 |
|
| 68 |
\pgfsetstateradius{1em} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
% Set the state radius for Moore states to #1. |
| 73 |
% |
| 74 |
% #1 = new state radius. |
| 75 |
% |
| 76 |
% Example: |
| 77 |
% |
| 78 |
% \pgfsetstatemooreradius{10pt} |
| 79 |
|
| 80 |
\def\pgfsetstatemooreradius#1{\setlength\pgf@mooreradius{#1}\ignorespaces} |
| 81 |
|
| 82 |
\pgfsetstatemooreradius{2em} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
% Set the distance between the outer and the inner circle in an |
| 87 |
% accepting state. |
| 88 |
% |
| 89 |
% #1 = new distance radius. |
| 90 |
% |
| 91 |
% Example: |
| 92 |
% |
| 93 |
% \pgfsetstatecircledistance{1.2pt} |
| 94 |
|
| 95 |
\def\pgfsetstatecircledistance#1{\setlength\pgf@doublethickness{#1}\ignorespaces} |
| 96 |
|
| 97 |
\pgfsetstatecircledistance{1.6pt} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
% Set the distance between a connecting line and a label to #1. |
| 102 |
% |
| 103 |
% #1 = new distance |
| 104 |
% |
| 105 |
% Example: |
| 106 |
% |
| 107 |
% \pgfsetstatelabeldistance{4pt} |
| 108 |
|
| 109 |
\def\pgfsetstatelabeldistance#1{\setlength\pgf@statebackoff{#1}\ignorespaces} |
| 110 |
|
| 111 |
\pgfsetstatelabeldistance{4pt} |
| 112 |
|
| 113 |
|
| 114 |
% Draws an accepting state #1 with name #3 at position #2. |
| 115 |
% |
| 116 |
% #1 = a name |
| 117 |
% #2 = a point |
| 118 |
% #3 = a text |
| 119 |
% |
| 120 |
% Example: |
| 121 |
% |
| 122 |
% \pgfstateaccepting{q1}{\pgfxy(1,1)}{$q_1$} |
| 123 |
|
| 124 |
\def\pgfstateaccepting#1#2#3{% |
| 125 |
\pgfnodecircle{#1}[stroke]{#2}{\pgf@stateradius}% |
| 126 |
\pgfputat{#2}{\pgfbox[center,center]{#3}}% |
| 127 |
{% |
| 128 |
\pgf@xa=\pgf@stateradius% |
| 129 |
\advance\pgf@xa by-\pgf@doublethickness% |
| 130 |
\pgfcircle[stroke]{#2}{\pgf@xa}}\ignorespaces} |
| 131 |
|
| 132 |
|
| 133 |
% Draws a rejecting state #1 with name #3 at position #2. |
| 134 |
% |
| 135 |
% #1 = a name |
| 136 |
% #2 = a point |
| 137 |
% #3 = a text |
| 138 |
% |
| 139 |
% Example: |
| 140 |
% |
| 141 |
% \pgfstaterejecting{q2}{\pgfstatedirection[n]{q1}}{$q_2$} |
| 142 |
|
| 143 |
\def\pgfstaterejecting#1#2#3{% |
| 144 |
\pgfnodecircle{#1}[stroke]{#2}{\pgf@stateradius}% |
| 145 |
\pgfputat{#2}{\pgfbox[center,center]{#3}}\ignorespaces} |
| 146 |
|
| 147 |
|
| 148 |
% Draws a Moore state #1 with name #3 and output #4 at position #2. |
| 149 |
% |
| 150 |
% #1 = a name |
| 151 |
% #2 = a point |
| 152 |
% #3 = a text |
| 153 |
% #4 = an output text |
| 154 |
% |
| 155 |
% Example: |
| 156 |
% |
| 157 |
% \pgfstatemoore{q}{\pgfxy(1,1)}{$q_1$}{$0,1$} |
| 158 |
|
| 159 |
\def\pgfstatemoore#1#2#3#4{% |
| 160 |
\pgfnodecircle{#1}[stroke]{#2}{\pgf@mooreradius}% |
| 161 |
\begin{pgftranslate}{#2}% |
| 162 |
\begin{pgfscope}% |
| 163 |
\pgfsetdash{{2pt}{1pt}}{0pt}% |
| 164 |
\pgfmoveto{\pgfpoint{-\pgf@mooreradius}{0cm}}% |
| 165 |
\pgflineto{\pgfpoint{\pgf@mooreradius}{0cm}}% |
| 166 |
\pgfqstroke% |
| 167 |
\end{pgfscope}% |
| 168 |
\pgfputat{\pgfpoint{0cm}{0.4\pgf@mooreradius}}{\pgfbox[center,base]{#3}}% |
| 169 |
\pgfputat{\pgfpoint{0cm}{-0.2\pgf@mooreradius}}{\pgfbox[center,top]{#4}}% |
| 170 |
\end{pgftranslate}\ignorespaces} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
% Connect states #1 and #2 and draw #4 above label, #3-th part of the |
| 176 |
% way from #1 to #2. |
| 177 |
% |
| 178 |
% #1 = a start state |
| 179 |
% #2 = an end state |
| 180 |
% #3 = a fraction of the distance between the two |
| 181 |
% #4 = a label |
| 182 |
% |
| 183 |
% Example: |
| 184 |
% |
| 185 |
% \pgfstateconnect{q1}{q2}[.5]{$0$} |
| 186 |
|
| 187 |
\def\pgfstateconnect#1#2[#3]#4{% |
| 188 |
\pgfnodeconnline{#1}{#2}% |
| 189 |
\pgfnodelabel{#1}{#2}[#3][\pgf@statebackoff]% |
| 190 |
{\pgfbox[center,bottom]{#4}}\ignorespaces} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
% Connect states #1 and #2 and draw #4 above label, #3-th part of the |
| 195 |
% way from #1 to #2. |
| 196 |
% |
| 197 |
% #1 = a start state |
| 198 |
% #2 = an end state |
| 199 |
% #3 = a fraction of the distance between the two |
| 200 |
% #4 = a label |
| 201 |
% |
| 202 |
% Example: |
| 203 |
% |
| 204 |
% \pgfstateconnectrotated{q1}{q2}[.5]{$0$} |
| 205 |
|
| 206 |
\def\pgfstateconnectrotated#1#2[#3]#4{% |
| 207 |
\pgfnodeconnline{#1}{#2}% |
| 208 |
\pgfnodelabelrotated{#1}{#2}[#3][\pgf@statebackoff]% |
| 209 |
{\pgfbox[center,bottom]{#4}}\ignorespaces} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
% Connect state #1 with itself with label #3 and a loop going in |
| 214 |
% direction #2. |
| 215 |
% |
| 216 |
% #1 = a state |
| 217 |
% #2 = an angle |
| 218 |
% #3 = a label |
| 219 |
% |
| 220 |
% Example: |
| 221 |
% |
| 222 |
% \pgfstateloop{q1}{90}{$0$} |
| 223 |
|
| 224 |
\def\pgfstateloop#1#2#3{% |
| 225 |
\c@pgf@counta=#2\relax% |
| 226 |
\c@pgf@countb=#2\relax% |
| 227 |
\advance\c@pgf@counta by15\relax% |
| 228 |
\advance\c@pgf@countb by-15\relax% |
| 229 |
\pgfnodeconncurve{#1}{#1}{\the\c@pgf@counta}{\the\c@pgf@countb}{2\pgf@stateradius}{2\pgf@stateradius}% |
| 230 |
\pgfputat{\pgfnodeborder{#1}{#2}{2.25\pgf@stateradius}}{\pgfbox[center,center]{#3}}\ignorespaces} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
% Connect states #1 and #2 and draw #4 above label, #3-th part of the |
| 235 |
% way from #1 to #2. |
| 236 |
% |
| 237 |
% #1 = a start state |
| 238 |
% #2 = an end state |
| 239 |
% #3 = a start angle |
| 240 |
% #4 = an end angle |
| 241 |
% |
| 242 |
% Example: |
| 243 |
% |
| 244 |
% \pgfstatecurve{q1}{q2}{10}{-10} |
| 245 |
|
| 246 |
\def\pgfstatecurve#1#2#3#4{% |
| 247 |
\pgfnodeconncurve{#1}{#2}{#3}{#4}{2\pgf@stateradius}{2\pgf@stateradius}\ignorespaces} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
% Give a position relative to some state. |
| 252 |
% |
| 253 |
% #1 = a direction |
| 254 |
% #2 = a state |
| 255 |
% #3 = a distance |
| 256 |
% |
| 257 |
% Example: |
| 258 |
% |
| 259 |
% \pgfstaterelative[east]{q2}{1cm} |
| 260 |
|
| 261 |
\def\pgfstaterelative{\pgfnoderelative} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
% Draw an initial state by drawing an inleading line. |
| 266 |
% |
| 267 |
% #1 = a state |
| 268 |
% #2 = a direction leading into the state (n,s,e,w) |
| 269 |
% #3 = a label (for example ``start'') |
| 270 |
% |
| 271 |
% Example: |
| 272 |
% |
| 273 |
% \pgfstateinitial{q1}[w]{start} |
| 274 |
|
| 275 |
\def\pgfstateinitial#1[#2]#3{{% |
| 276 |
\pgfmoveto{\pgfrelative{\pgfnodeborder{#1}{\pgfdirection{#2}}{-2pt}}% |
| 277 |
{\pgfpolar{\pgfdirection{#2}}{1.5\pgf@stateradius}}}% |
| 278 |
\pgflineto{\pgfnodeborder{#1}{\pgfdirection{#2}}{\pgf@nodesepend}}% |
| 279 |
\pgfstroke% |
| 280 |
\edef\temp{[\csname pgf@statepos\pgfdirection{#2}\endcsname]}% |
| 281 |
\pgfputat{\pgfrelative{\pgfnodeborder{#1}{\pgfdirection{#2}}{0pt}}% |
| 282 |
{\pgfpolar{\pgfdirection{#2}}{1.5\pgf@stateradius}}}% |
| 283 |
{\expandafter\pgfbox\temp{#3}}}\ignorespaces} |
| 284 |
|
| 285 |
\expandafter\def\csname pgf@statepos0\endcsname{left,center} |
| 286 |
\expandafter\def\csname pgf@statepos90\endcsname{center,bottom} |
| 287 |
\expandafter\def\csname pgf@statepos180\endcsname{right,center} |
| 288 |
\expandafter\def\csname pgf@statepos270\endcsname{center,top} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
% Draw an final state by drawing an outleading line. |
| 293 |
% |
| 294 |
% #1 = a state |
| 295 |
% #2 = a direction leading out of the state (valid directions are |
| 296 |
% n,s,e,w) |
| 297 |
% #3 = a label (for example ``exit'') |
| 298 |
% |
| 299 |
% Example: |
| 300 |
% |
| 301 |
% \pgfstatefinal{q1}[right]{accept} |
| 302 |
|
| 303 |
\def\pgfstatefinal#1[#2]#3{{% |
| 304 |
\pgfmoveto{\pgfnodeborder{#1}{\pgfdirection{#2}}{\pgf@nodesepstart}}% |
| 305 |
\pgflineto{\pgfrelative{\pgfnodeborder{#1}{\pgfdirection{#2}}{-\pgf@nodesepend}}% |
| 306 |
{\pgfpolar{\pgfdirection{#2}}{1.5\pgf@stateradius}}}% |
| 307 |
\pgfstroke% |
| 308 |
\edef\tempb{[\csname pgf@statepos\pgfdirection{#2}\endcsname]}% |
| 309 |
\pgfputat{\pgfrelative{\pgfnodeborder{#1}{\pgfdirection{#2}}{0pt}}% |
| 310 |
{\pgfpolar{\pgfdirection{#2}}{1.5\pgf@stateradius}}}% |
| 311 |
{\expandafter\pgfbox\tempb{#3}}}\ignorespaces} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
\endinput |