-
Notifications
You must be signed in to change notification settings - Fork 5
/
demo.tex
executable file
·302 lines (276 loc) · 14.1 KB
/
demo.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
% 使用ctexart文档类(用XeLaTeX编译,直接支持中文)
\documentclass{ctexart}
%导言区,可以在此引入必要的宏包
\usepackage{tikz-flowchart}
% 各参数默认值
\flowchartset{
free color = green, % 自由连线颜色(默认取green)
norm color = blue, % 常规连线颜色(默认取blue)
cong color = red, % 关联连线颜色(默认取red)
proc fill color = white, % 顺序处理框填充颜色(默认取白色)
test fill color = white, % 判断框填充颜色(默认取白色)
io fill color = white, % 输入/输出框填充颜色(默认取白色)
term fill color = white, % 开始/结束框填充颜色(默认取白色)
proc text width = 8em, % 顺序处理框宽度(默认取8em)
test text width = 5em, % 判断框宽度(默认取5em)
io text width = 6em, % 输入/输出框宽度(默认取6em)
term text width = 3em, % 开始/结束宽度(默认取3em)
chain direction = below, % 结点自动布置方向(默认取below)
minimum node distance = 6mm, % 最小结点间距(默认取6mm)
maximum node distance = 60mm, % 最大结点间距(默认取60mm)
border line width = \pgflinewidth, % 各类流程框边框宽度(默认取当前线条宽度)
flow line width = \pgflinewidth, % 各类流程线线条宽度(默认取当前线条宽度)
stealth length = 1.5mm, % 箭头长度(默认取1.5mm)
stealth width = 1.0mm, % 箭头宽度(默认取1.0mm)
}
\begin{document} %在document环境中撰写文档
% 可以绘制复杂流程图
\begin{tikzpicture}
% -------------------------------------------------
% 布置结点单元
\node [proc, densely dotted, it] (p0) {新触发器消息线程};
% 使用"join"参数以将新结点自动连接到前一个结点
\node [term, join] {触发器调度表};
\node [proc, join] (p1) {获取配额 $k > 1$};
\node [proc, join] {打开队列};
\node [proc, join] {派发消息};
\node [test, join] (t1) {获得消息?};
% test结点的出口连接相对复杂,不再使用"join"参数
% 继续布置完成所有结点
\node [proc] (p2) {$k \mathbin{{-}{=}} 1$};
\node [proc, join] (p3) {派发消息};
\node [test, join] (t2) {获得消息?};
\node [test] (t3) {容量满?};
\node [test] (t4) {$k \mathbin{{-}{=}} 1$};
% 将下一个结点布置到第2栏,直接布置于p1结点的右边
% 由于栏间距已进行设置,在此无需再次设定
\node [proc, fill=\freecolor!25, right=of p1] (p4) {重置拥塞};
\node [proc, join=by free] {设置等待标志\textsc{mq}};
\node [proc, join=by free] (p5) {派发消息};
\node [test, join=by free] (t5) {获得消息?};
\node [test] (t6) {容量满?};
% 布置更多结点(应该避免这样布置结点,不是很好看)
\node [test] (t7) [right=of t2] {$k \mathbin{{-}{=}} 1$};
\node [proc, fill=\congcolor!25, right=of t3] (p8) {设置拥塞};
\node [proc, join=by cong, right=of t4] (p9) {关闭队列};
\node [term, join] (p10) {退出触发器消息线程};
% -------------------------------------------------
% 布置坐标点,以实现拐角连接或添加注释说明
% 同时,用\cmark命令对各个点进行了标记,以方便调试
\node [coord, right=0.8 of t1] (c1) {}; \cmark{1}
\node [coord, right=0.8 of t3] (c3) {}; \cmark{3}
\node [coord, right=0.8 of t6] (c6) {}; \cmark{6}
\node [coord, right=0.8 of t7] (c7) {}; \cmark{7}
\node [coord, left=0.8 of t4] (c4) {}; \cmark{4}
\node [coord, right=0.8 of t4] (c4r) {}; \cmark[r]{4}
\node [coord, left=0.8 of t7] (c5) {}; \cmark{5}
% -------------------------------------------------
% 布置注释结点
\node [above=0mm of p4, it] {(队列为空)};
\node [above=0mm of p8, it] {(队列非空)};
% -------------------------------------------------
% 首先绘制判断框与其它结点单元南北方向的连接线
% 每次绘制时,先绘制一个带有一个固定位置标注的路径(path)
% 然后再绘制箭头本身(arrow)。
\path (t1.south) to node [near start, xshift=1em] {$y$} (p2);
\draw [norm] (t1.south) -- (p2);
\path (t2.south) to node [near start, xshift=1em] {$y$} (t3);
\draw [norm] (t2.south) -- (t3);
\path (t3.south) to node [near start, xshift=1em] {$y$} (t4);
\draw [norm] (t3.south) -- (t4);
\path (t5.south) to node [near start, xshift=1em] {$y$} (t6);
\draw [free] (t5.south) -- (t6);
\path (t6.south) to node [near start, xshift=1em] {$y$} (t7);
\draw [free] (t6.south) -- (t7);
% -------------------------------------------------
% 其次绘制判断框与其它结点单元东西方向的连接线
% 为固定标位置,在垂直方向上进行坐标设定,然后再绘制箭头本身(arrow)。
\path (t3.east) to node [near start, yshift=1em] {$n$} (c3);
\draw [cong] (t3.east) -- (p8);
\path (t4.east) to node [yshift=-1em] {$k \leq 0$} (c4r);
\draw [norm] (t4.east) -- (p9);
% -------------------------------------------------
% 最后绘制其它方向的连接线
% 同样先绘制一个带有一个固定位置标注的路径(path),然后再绘制箭头本身(arrow)。
\path (t1.east) to node [near start, yshift=1em] {$n$} (c1);
\draw [free] (t1.east) -- (c1) |- (p4);
\path (t2.east) -| node [very near start, yshift=1em] {$n$} (c1);
\draw [free] (t2.east) -| (c1);
\path (t4.west) to node [yshift=-1em] {$k>0$} (c4);
\draw [norm] (t4.west) -- (c4) |- (p3);
\path (t5.east) -| node [very near start, yshift=1em] {$n$} (c6);
\draw [free] (t5.east) -| (c6);
\path (t6.east) to node [near start, yshift=1em] {$n$} (c6);
\draw [free] (t6.east) -| (c7);
\path (t7.east) to node [yshift=-1em] {$k \leq 0$} (c7);
\draw [free] (t7.east) -- (c7) |- (p9);
\path (t7.west) to node [yshift=-1em] {$k>0$} (c5);
\draw [free] (t7.west) -- (c5) |- (p5);
% -------------------------------------------------
% 最后一个标不符合以上所有规则
\draw [->, dotted, thick, shorten >=1mm]
(p9.south) -- ++(5mm,-3mm) -- ++(27mm,0)
|- node [black, near end, yshift=0.75em, it]
{(当消息和资源都有效时)} (p0);
% -------------------------------------------------
\end{tikzpicture}
% 可以在结点中使用\verb等命令
\begin{tikzpicture}
% 布置结点单元
\node [term] (st) {开始};
\node [proc, join] (p1) {\verb!int r = 0!};
\node [test, join] (t1) {\verb!m < n!};
\node [proc] (p2) {\verb! m ^= n !\\ \verb!n ^= m !\\ \verb!m ^= n!};
\node [test, join] (t2) {\verb|n != 0|};
\node [proc] (p3) {\verb!r = m % n! \\ \verb!m = n!\\ \verb!n = r!};
\node [proc] (p4) {\verb!return m!};
\node [term, join] (end) {结束};
% 布置用于连接的坐标结点,同时为其布置调试标记点。
\node [coord] (c1) at ($(p2.south)!0.5!(t2.north)$) {}; \cmark{1}
\node [coord, below = 0.25 of p3] (c2) {}; \cmark{2}
\node [coord, above = 0.25 of p4] (c3) {}; \cmark{3}
\node [coord, right = 1.0 of t1] (c4) {}; \cmark{4}
\node [coord, right = 1.0 of t2] (c5) {}; \cmark{5}
\node [coord, left = 1.0 of t2] (ct) {}; \cmark{t}
\node [coord] (c6) at (ct |- c2) {}; \cmark{6}
% 判断框连线,每次绘制时,先绘制一个带有一个固定
% 位置标注的路径(path),然后再绘制箭头本身(arrow)。
\path (t1.south) -- node [near start, right] {$Y$} (p2.north);
\draw [norm] (t1.south) -- (p2.north);
\path (t1.east) -- node [near start, above] {$N$} (c4);
\draw [norm] (t1.east) -- (c4) |- (c1);
\path (t2.south) -- node [near start, right] {$Y$} (p3.north);
\draw [norm] (t2.south) -- (p3.north);
\path (t2.east) -- node [near start, above] {$N$} (c5);
\draw [norm] (t2.east) -- (c5) |- (c3) -- (p4.north);
% 其它连线
\draw [norm](p3.south) |- (c6) |- (c1);
\end{tikzpicture}
% 可以更改字号、各类流程框的宽度等
\begin{tikzpicture}[font=\small]
% 布置结点单元
\node [term] (st) {开始};
\node [proc, join] (p1) {\verb|sum = x|\\\verb|x_pow = x|\\\verb|item = 0.0|};
\node [proc, join] (p2) {\verb|n = 1|\\\verb|fact = 1|\\\verb|sign = 1|};
\node [proc, join, text width = 16em] (p3) {\verb|fact = fact * (n + 1) * (n + 2)|\\\verb|x_pow *= x * x|\\\verb|sign = -sign|\\\verb|item = sign * x_pow / fact|\\\verb|sum += item|\\\verb|n += 2|};
\node [test, join, text width = 10em] (t1) {\verb|fabs(item) > epsilon|};
\node [proc] (p4) {\verb|return sum|};
\node [term, join] (end) {结束};
% 布置用于连接的坐标结点,同时为其布置调试标记点。
\node [coord] (c1) at ($(p2.south)!0.5!(p3.north)$) {}; \cmark{1}
\node [coord, right = 2.0 of t1] (ct) {}; \cmark{t}
\node [coord] (c2) at (ct |- c1) {}; \cmark{2}
% 判断框连线,每次绘制时,先绘制一个带有一个固定
% 位置标注的路径(path),然后再绘制箭头本身(arrow)。
\path (t1.south) -- node [near start, right] {$N$} (p4.north);
\draw [norm] (t1.south) -- (p4.north);
\path (t1.east) -- node [near start, above] {$Y$} (ct);
\draw [norm] (t1.east) -| (c2) -- (c1);
\end{tikzpicture}
% 可以局部更改各参数
\flowchartset{
proc fill color = orange!10, % 顺序处理框填充颜色(默认取白色)
test fill color = green!30, % 判断框填充颜色(默认取白色)
io fill color = blue!30, % 输入/输出框填充颜色(默认取白色)
term fill color = red!30, % 开始/结束框填充颜色(默认取白色)
}
\begin{tikzpicture}
% 布置结点单元
\node [term] (st) {开始};
\node [proc, text width = 6em, join] (p1) {\verb|int divisor|};
\node [test, join] (t1) {\verb|n <= 1|};
\node [proc, text width = 6em] (p2) {\verb|divisor = 2|};
\node [test, text width = 10em, join] (t2) {\verb|divisor * divisor <= n|};
\node [test, text width = 8em] (t3) {\verb|n % divisor == 0|};
\node [proc, text width = 6em] (p3) {\verb|divisor++|};
\node [term, below = 1.6 of p3] (end) {结束};
\node [proc, text width = 6em, left = 4.8 of t2] (p4) {\verb|return 0|};
\node [proc, text width = 6em, right = 3.5 of p3] (p5) {\verb|return 0|};
\node [proc, text width = 6em, right = 5.8 of t3] (p6) {\verb|return 1|};
% 布置用于连接的坐标结点,同时为其布置调试标记点。
\node [coord] (c1) at ($(p2.south)!0.5!(t2.north)$) {}; \cmark{1}
\node [coord, below = 0.25 of p3] (c2) {}; \cmark{2}
\node [coord, above = 0.5 of end] (c3) {}; \cmark{3}
\node [coord, left = 0.5 of t2] (ct) {}; \cmark{t}
\node [coord] (c4) at (c3 -| p5) {}; \cmark{4}
\node [coord] (c5) at (c2 -| ct) {}; \cmark{5}
% 判断框连线,每次绘制时,先绘制一个带有一个固定
% 位置标注的路径(path),然后再绘制箭头本身(arrow)。
\path (t1.south) -- node [near start, right] {$N$} (p2.north);
\draw [norm] (t1.south) -- (p2.north);
\path (t1.west) -| node [near start, above] {$Y$} (p4.north);
\draw [norm] (t1.west) -| (p4.north);
\path (t2.south) -- node [near start, right] {$Y$} (t3.north);
\draw [norm] (t2.south) -- (t3.north);
\path (t2.east) -| node [near start, above] {$N$} (p6.north);
\draw [norm] (t2.east) -| (p6.north);
\path (t3.south) -- node [near start, right] {$N$} (p3.north);
\draw [norm] (t3.south) -- (p3.north);
\path (t3.east) -| node [near start, above] {$Y$} (p5.north);
\draw [norm] (t3.east) -| (p5.north);
% 其它连线
\draw [norm](p3.south) |- (c5) |- (c1);
\draw [norm](p4.south) |- (c3);
\draw [norm](p4.south) |- (c3) -- (end);
\draw [norm](p5.south) -- (c4);
\draw [norm](p6.south) |- (c3);
\draw [norm](p6.south) |- (c3) -- (end);
\end{tikzpicture}
% 测试文字宽度及非连接交点
% 各参数默认值
\flowchartset{
proc text width = 4em, % 顺序处理框宽度(默认取8em)
test text width = 3em, % 判断框宽度(默认取5em)
}
\begin{tikzpicture}
% 布置结点单元
\node [proc] (p1){\verb|d = 2|};
\node [test, join] (t1) {\verb|a < n|?};
\node [test] (t2) {\verb|n % d == 0|};
\node [proc] (p2) {\verb|d++|};
\node [proc,below = 1.5 of p2] (p3) {\verb|d = 2|};
\node [test, join] (t3) {\verb|d < n|?};
\node [proc, left = 2.0 of t3, shift={(0cm, -0.8cm)}] (p4) {\verb|z = d|};
\node [proc, right = 2.0 of t3, shift={(0cm, -0.8cm)}] (p5) {\verb|z = n|};
% 布置用于连接的坐标结点,同时为其布置调试标记点。
\node [coord, above = 0.5 of p1] (c1) {};% \cmark{1}
\node [coord] (c2) at ($(p1)!0.35!(t1)$) {};% \cmark{2}
\node [coord, below = 0.25 of p2] (c3) {};% \cmark{3}
\node [coord, above = 0.45 of p3] (c4) {};% \cmark{4}
\node [coord] (c5) at ($(p3)!0.35!(t3)$) {};% \cmark{5}
\node [coord, below = 0.5 of p4] (c6) {};% \cmark{6}
\node [coord] (c7) at (c6 -| p4) {};% \cmark{7}
\node [coord] (c8) at (c6 -| t3) {};% \cmark{8}
\node [coord, right = 1.0 of t1] (c9) {};% \cmark{9}
\node [coord] (c10) at (c9 |- t2) {};% \cmark{10}
\node [coord, right = 0.5 of c10] (c11) {};% \cmark{11}
\node [coord, left = 0.8 of t1] (ct) {};% \cmark{t}
\node [coord] (c12) at (c3 -| ct) {};% \cmark{12}
\node [coord, below = 0.5 of c8] (c13) {};% \cmark{13}
% 判断框连线,每次绘制时,先绘制一个带有一个固定
% 位置标注的路径(path),然后再绘制箭头本身(arrow)。
\path (t1.south) -- node [near start, right] {$Y$} (t2.north);
\draw [norm] (t1.south) -- (t2.north);
\path (t1.east) -- node [near start, above] {$N$} (c9);
\draw [norm] (t1.east) -- (c9) |- (c4) -- (p3.north);
\path (t2.south) -- node [near start, right] {$N$} (p2.north);
\draw [norm] (t2.south) -- (p2.north);
\path (t2.east) -- node [near start, above] {$Y$} (c10);
\draw [lnorm] (t2.east) -- ($(c10) + (-2pt, 0)$);
\path (t3.west) -| node [near start, above] {$Y$} (p4.north);
\draw [norm] (t3.west) -| (p4.north);
\path (t3.east) -| node [near start, above] {$N$} (p5.north);
\draw [norm] (t3.east) -| (p5.north);
% 其它连线
\draw [norm] (c1) -- (p1);
\draw [norm] (p2.south) |- (c12) |- (c2);
% 绘制不相交的两个交汇路径,注意connect样式的使用
\draw [norm, connect=(t2.east) to (c11) over (c10) by 3pt] |- (c5);
\draw [norm] (p4.south) |- (c8) -- (c13);
\draw [norm] (p5.south) |- (c8) -- (c13);
\end{tikzpicture}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End: