避免自动机 TikZ 中的箭头相互交叉

避免自动机 TikZ 中的箭头相互交叉

我想知道是否有办法避免箭头像下图中那样交叉。箭头从“帐户”、“密码”和“验证”开始,我想绕过“日志”,这样就不会出现箭头交叉的情况。代码也在下面,提前谢谢!

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata,arrows}
\begin{document}
\begin{figure}
\hspace{-3cm}
\begin{tikzpicture}[
->,>=stealth',
shorten >=1pt,
auto,
node distance=5cm,
thick,
transform shape,
%main state/.style={}
%state/.style={font=\sffamily\Large\bfseries}
buffer/.style={rectangle,draw,minimum width=2cm,minimum height=1cm,align=center}
]
  \node[state,initial,accepting,minimum size=2cm] (1) {Idle};
  \node[state] (2) [right of=1] {Select Fuel};
  \node[state] (3) [right of=2] {Payment};
  \node[state] (4) [below right of=3] {Account};
  \node[state] (5) [below left of=4]{PIN};
  \node[state] (6) [below of=3]{Cash};
  \node[state] (7) [left of=5]{Verify};
  \node[state] (8) [left of=6]{Pump};
  \node[state] (9) [left of=8]{Log};
 % \node[buffer] (10) [left of=9]{buffer};
  \path[every node/.style={font=\sffamily\scriptsize}]
    (1) edge [bend left] node[above] {PressStar/} (2)
    (2) edge [bend left] node[above] {PressStar/} (3)
        edge [loop above] node[above] {Press2/FuelSelect95} (2)
        edge [loop left] node [below,yshift=-8pt] {Press1/FuelSelect92} (2)
        edge [loop right] node [below,yshift=-8pt] {Press3/FuelSelectOC} (2)
    (3) edge [bend left] node[right] {ChooseAcc/} (4)
        edge [bend left] node[left] {InstertCash/IncCash} (6)
    (4) edge [bend left] node[right] {NumberEnter6/StoreAccNo} (5)
        edge [bend left] node[right] {TimeOut15/} (1)
    (5) edge [bend left] node[below] {NumberEnter4/StorePIN} (7)
        edge [bend left] node[above,yshift=36pt,xshift=-28pt] {TimeOut15/} (1)
    (6) edge [loop below] node {InsertCash/IncCash} (6)
        edge [bend left] node { } (8)
    (7) edge [bend left] node[right] {NotVerified/} (1)
        edge [bend left] node[right,yshift=-5pt] {Verified/} (8)
    (8) edge [bend left] node {LeftOL,ReachMaxFuel,TimeOut15/} (9)
    (9) edge [bend left] node {PressStar/} (1);
\end{tikzpicture}
\end{figure}
\end{document}

答案1

这是一个可能的解决方案bend left=130,looseness=2;我还添加了一个pgfinterruptboundingbox环境以避免边界框由于该选项而变得太大looseness

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata,arrows}
\begin{document}
\begin{figure}
\hspace{-3cm}
\begin{tikzpicture}[
->,>=stealth',
shorten >=1pt,
auto,
node distance=5cm,
thick,
transform shape,
%main state/.style={}
%state/.style={font=\sffamily\Large\bfseries}
buffer/.style={rectangle,draw,minimum width=2cm,minimum height=1cm,align=center}
]
  \node[state,initial,accepting,minimum size=2cm] (1) {Idle};
  \node[state] (2) [right of=1] {Select Fuel};
  \node[state] (3) [right of=2] {Payment};
  \node[state] (4) [below right of=3] {Account};
  \node[state] (5) [below left of=4]{PIN};
  \node[state] (6) [below of=3]{Cash};
  \node[state] (7) [left of=5]{Verify};
  \node[state] (8) [left of=6]{Pump};
  \node[state] (9) [left of=8]{Log};
 % \node[buffer] (10) [left of=9]{buffer};
  \begin{pgfinterruptboundingbox}
  \path[every node/.style={font=\sffamily\scriptsize}]
    (1) edge [bend left] node[above] {PressStar/} (2)
    (2) edge [bend left] node[above] {PressStar/} (3)
        edge [loop above] node[above] {Press2/FuelSelect95} (2)
        edge [loop left] node [below,yshift=-8pt] {Press1/FuelSelect92} (2)
        edge [loop right] node [below,yshift=-8pt] {Press3/FuelSelectOC} (2)
    (3) edge [bend left] node[right] {ChooseAcc/} (4)
        edge [bend left] node[left] {InstertCash/IncCash} (6)
    (4) edge [bend left] node[right] {NumberEnter6/StoreAccNo} (5)
        edge [bend left=130,looseness=2] node[right] {TimeOut15/} (1)
    (5) edge [bend left] node[below] {NumberEnter4/StorePIN} (7)
        edge [bend left] node[above,yshift=36pt,xshift=-28pt] {TimeOut15/} (1)
    (6) edge [loop below] node {InsertCash/IncCash} (6)
        edge [bend left] node { } (8)
    (7) edge [bend left] node[right] {NotVerified/} (1)
        edge [bend left] node[right,yshift=-5pt] {Verified/} (8)
    (8) edge [bend left] node {LeftOL,ReachMaxFuel,TimeOut15/} (9)
    (9) edge [bend left] node {PressStar/} (1);
  \end{pgfinterruptboundingbox}
\end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

另一种方法是使用out=300,in=220,looseness=2

答案2

我试图避免箭头交叉,仍然对箭头进行一些抛光,使它们看起来像一个圆圈。

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata,arrows,calc}
\begin{document}
\begin{figure}
\hspace{-4cm}
\begin{tikzpicture}[
->,>=stealth',
shorten >=1pt,
auto,
node distance=5cm,
thick,
transform shape,
%main state/.style={}
%state/.style={font=\sffamily\Large\bfseries}
buffer/.style={rectangle,draw,minimum width=2cm,minimum height=1cm,align=center}
]

\xdef\Rad{7}

  \node[state,initial,accepting,minimum size=2cm] (1) at (90:\Rad) {Idle};
  \node[state] (2) at (30:\Rad) {Select Fuel};
  \node[state] (3) at (-30:\Rad) {Payment};
  \node[state] (4) at (90:.5*\Rad) {Account};
  \node[state] (5) at (-30:.5*\Rad) {PIN};
  \node[state] (6) at (-90:\Rad){Cash};
  \node[state] (7) at (-150:.5*\Rad) {Verify};
  \node[state] (8) at (-150:\Rad){Pump};
  \node[state] (9) at (-210:\Rad) {Log};
 % \node[buffer] (10) [left of=9]{buffer};

 \tikzset{every node/.style={font=\sffamily\scriptsize}}
  \path[draw]
    (1) edge [bend left] node[above right,pos=.4] {PressStar/} (2)
    (2) edge [bend left] node[right] {PressStar/} (3)
        edge [loop above] node[above] {Press2/FuelSelect95} (2)
        edge [loop left] node [below,yshift=-8pt] {Press1/FuelSelect92} (2)
        edge [loop right] node [below,yshift=-8pt] {Press3/FuelSelectOC} (2)
    (3) %edge [bend left] node[right] {ChooseAcc/} (4)
        edge [bend left] node[left] {InstertCash/IncCash} (6)
    (4) to[out=-80] node[left,text width=1.5cm] {NumberEnter6/\\StoreAccNo} (5)
        %edge [bend left] node[right] {TimeOut15/} (1)
    (5) edge [bend left] node[below] {NumberEnter4/StorePIN} (7)
        %edge [bend left] node[above,yshift=36pt,xshift=-28pt] {TimeOut15/} (1)
    (6) edge [loop below] node {InsertCash/IncCash} (6)
        edge [bend left] node { } (8)
    (7) %edge [bend left] node[right] {NotVerified/} (1)
        edge [bend left] node[right,yshift=-5pt] {Verified/} (8)
    (8) edge [bend left] node[text width=1.3cm] {LeftOL,\\ReachMaxFuel,\\TimeOut15/} (9)
    (9) edge [bend left] node {PressStar/} (1);

\draw (4) to[in=-120,out=120] node[right] {TimeOut15/} (1) ;
\draw (7) to[in=210,out=120] node[pos=.3] {NotVerified/} (1) ;
\draw (5) to[in=220,out=180] node[pos=.55,left] {TimeOut15/} (1) ;
\draw (3) to[in=-60,out=100] node[right] {ChooseAcc/} (4) ;

\end{tikzpicture}
\end{figure}
\end{document}

相关内容