我正在尝试使用 Tikz 绘制程序的控制流图,并希望渲染像下图这样的输出,但我无法在给定的图中绘制弯曲边缘。
用于相同的代码(但用于不同的程序)
\begin{figure}
\begin{tikzpicture}
[%
->,
shorten >=2pt,
>=stealth,
node distance=1cm,
noname/.style={%
rounded corners=0.5em,
minimum width=5em,
minimum height=3em,
draw,
}
]
\node[noname] (1) {$Start : P$};
\node[noname] (2) [below=of 1] {$c=3$};
\node[noname] (3) [node distance=1cm and 3mm,below left=of 2] {$b = a*b$};
\node[noname] (4) [node distance=1cm and 3mm,below right=of 2]{print $a*c$};
\node[noname] (5) [node distance=3cm,below=of 4] {$a=b*c$};
\node[noname] (6) [below=of 3] {print $a*b$};
\node[noname] (7) [below=of 6] {$Call : P$};
\node[noname] (8) [node distance=1cm,below=of 7] {$Return : P$};
\node[noname] (9) [below right=of 8] {$Exits : P$};
\node[noname] (10) [node distance=6cm,right =of 1] {$Start : main$};
\node[noname] (11) [below=of 10] {$a = 3$};
\node[noname] (12) [below=of 11] {$b = a*b$};
\node[noname] (13) [below=of 12] {$Call : P$};
\node[noname] (14) [below=of 13] {$Return : P$};
\node[noname] (15) [below=of 14] {$c = a*b$};
\node[noname] (16) [below=of 15] {$Exit : main$};
\path (1) edge node {} (2)
(2) edge node {} (3)
(2) edge node {} (4)
(3) edge node {} (6)
(4) edge node {} (5)
(6) edge node {} (7)
(7) edge node {} (8)
(8) edge node {} (9)
(5) edge [bend left = 2em] node {} (9)
(10) edge node {} (11)
(11) edge node {} (12)
(12) edge node {} (13)
(13) edge node {} (14)
(14) edge node {} (15)
(15) edge node {} (16)
(13) edge [bend right = 1.4em] node {} (1)
(9) edge [bend left = 2.1em] node {} (14)
\end{tikzpicture}
\caption{Supergraph for given program}
\label{fig:1}
\end{figure}
但它输出的图像如下:
我怎样才能改变节点的边缘致电:P到节点开始 : P和节点退出 : P到节点返回 : P到上面所需图中绘制的那个
答案1
您可以使用 的in
和out
选项edge
,这可以让您定义路径与节点相交的角度。例如out=45
表示路径从起始节点向右上方延伸,而in=45
表示路径从结束节点的右上方进入。
dotted
将使线变成虚线。
因此,如果你将图中的最后两个边改为
(13) edge [out=230,in=45,dotted] node {} (1)
(9) edge [out=-30,in=180,dotted] node {} (14);
你得到:
下面是完整代码。(当然,你不需要为所有边添加空节点,因此我删除了node{}
。)
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
[%
->,
shorten >=2pt,
>=stealth,
node distance=1cm,
noname/.style={%
rounded corners=0.5em,
minimum width=5em,
minimum height=3em,
draw,
}
]
\node[noname] (1) {$Start : P$};
\node[noname] (2) [below=of 1] {$c=3$};
\node[noname] (3) [node distance=1cm and 3mm,below left=of 2] {$b = a*b$};
\node[noname] (4) [node distance=1cm and 3mm,below right=of 2]{print $a*c$};
\node[noname] (5) [node distance=3cm,below=of 4] {$a=b*c$};
\node[noname] (6) [below=of 3] {print $a*b$};
\node[noname] (7) [below=of 6] {$Call : P$};
\node[noname] (8) [node distance=1cm,below=of 7] {$Return : P$};
\node[noname] (9) [below right=of 8] {$Exits : P$};
\node[noname] (10) [node distance=6cm,right =of 1] {$Start : main$};
\node[noname] (11) [below=of 10] {$a = 3$};
\node[noname] (12) [below=of 11] {$b = a*b$};
\node[noname] (13) [below=of 12] {$Call : P$};
\node[noname] (14) [below=of 13] {$Return : P$};
\node[noname] (15) [below=of 14] {$c = a*b$};
\node[noname] (16) [below=of 15] {$Exit : main$};
\path (1) edge (2)
(2) edge (3)
(2) edge (4)
(3) edge (6)
(4) edge (5)
(6) edge (7)
(7) edge (8)
(8) edge (9)
(5) edge [bend left = 2em] (9)
(10) edge (11)
(11) edge (12)
(12) edge (13)
(13) edge (14)
(14) edge (15)
(15) edge (16)
(13) edge [out=230,in=45,dotted] (1)
(9) edge [out=-30,in=180,dotted] (14);
\end{tikzpicture}
\end{document}
答案2
使用.. controls <first control point> and <second control point> .. <end point>
\begin{tikzpicture}
[%
->,
shorten >=2pt,
>=stealth,
node distance=1cm,
noname/.style={%
rounded corners=0.5em,
minimum width=5em,
minimum height=3em,
draw,
}
]
\node[noname] (1) {$Start : P$};
\node[noname] (2) [below=of 1] {$c=3$};
\node[noname] (3) [node distance=1cm and 3mm,below left=of 2] {$b = a*b$};
\node[noname] (4) [node distance=1cm and 3mm,below right=of 2]{print $a*c$};
\node[noname] (5) [node distance=3cm,below=of 4] {$a=b*c$};
\node[noname] (6) [below=of 3] {print $a*b$};
\node[noname] (7) [below=of 6] {$Call : P$};
\node[noname] (8) [node distance=1cm,below=of 7] {$Return : P$};
\node[noname] (9) [below right=of 8] {$Exits : P$};
\node[noname] (10) [node distance=6cm,right =of 1] {$Start : main$};
\node[noname] (11) [below=of 10] {$a = 3$};
\node[noname] (12) [below=of 11] {$b = a*b$};
\node[noname] (13) [below=of 12] {$Call : P$};
\node[noname] (14) [below=of 13] {$Return : P$};
\node[noname] (15) [below=of 14] {$c = a*b$};
\node[noname] (16) [below=of 15] {$Exit : main$};
\draw (1) edge (2)
(2) edge (3)
(2) edge (4)
(3) edge (6)
(4) edge (5)
(6) edge (7)
(7) edge (8)
(8) edge (9)
(5) edge [bend left = 2em] (9)
(10) edge (11)
(11) edge (12)
(12) edge (13)
(13) edge (14)
(14) edge (15)
(15) edge (16);
\draw[->] (13) .. controls +(left:4cm) and +(right:4cm) .. (1);
\draw[->] (9)[anchor=east] .. controls +(right:3cm) and +(left:5cm) .. (14);
\end{tikzpicture}