我有这个代码:
% Define block styles
\tikzstyle{C} = [diamond, draw, fill=blue!20, text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{I} = [chamfered rectangle, minimum width=1.5cm, minimum height=1cm, draw,chamfered rectangle corners=north west]
\tikzstyle{P} = [chamfered rectangle, minimum width=1.5cm, minimum height=1cm, draw,chamfered rectangle corners=south east]
\tikzstyle{V} = [rectangle, draw, minimum width=1.5cm, minimum height=1cm]
\tikzstyle{S} = [draw, ellipse,fill=red!70, node distance=3cm, minimum height=2em]
\tikzstyle{line} = [draw, -latex']
%------------------------------------------------
\begin{center}
\begin{tikzpicture}
%nodes
\node [S] (S1) {\textbf{START}};
\node [I, below of=S1,node distance=1.5cm] (I1) {$ x $};
\node [I, below of=I1,node distance=1.5cm] (I2) {$ n $};
\node [C, below of=I2,node distance=2.1cm] (C1) {$ n\>0 $};
\node [V, below of=C1,node distance=2.1cm] (V1) {$ i\leftarrow 1 $};
\node [C, below of=V1,node distance=2.1cm] (C2) {$ i\<n $};
\node [S, right of=C2,node distance=2.7cm] (S2) {\textbf{STOP}};
\node [V, below of=C2,node distance=2.1cm] (V2) {$ y=x+i $};
\node [P, below of=V2,node distance=1.5cm] (P1) {$ y $};
\node [V, below of=P1,node distance=1.5cm] (V3) {$ i\leftarrow i+1 $};
%arrows
\begin{scope} [every path/.style=line]
\path (S1) -- (I1);
\path (I1) -- (I2);
\path (I2) -- (C1);
\path (C1) -- (V1);
\path (V1) -- (C2);
\path (C2) -- (V2); \path (C2) -- (S2);
\path (V2) -- (P1);
\path (P1) -- (V3);
\end{scope}
\end{tikzpicture}
\end{center}
我有这个输出:
如何获取输出:
换句话说,我需要哪种命令才能获得蓝色箭头?
答案1
一种替代解决方案,利用chains
库功能。
编辑:
如果您希望通过join
选项确定一些线条,则第一个解决方案将扩展新的join
线条类型和样式,从而可以使用
\documentclass[tikz,
border=5mm]{standalone}
\usetikzlibrary{arrows, chains, positioning, shapes, shapes.misc}
\makeatletter
\tikzset{suppress join/.code={\def\tikz@after@path{}}}
\makeatother
%------------------------------------------------
\begin{document}
\begin{tikzpicture}[
node distance=5 mm and 7mm,
start chain = going below,
base/.style = {draw, minimum width=1.5cm, minimum height=1cm,
align=center, on chain, join=by ->},
C/.style = {base, diamond, fill=blue!20, inner sep=0pt},
I/.style = {base, chamfered rectangle,
chamfered rectangle corners=north west},
P/.style = {base,chamfered rectangle, draw,chamfered rectangle corners=south east},
V/.style = {base, rectangle},
S/.style = {base, ellipse, fill=red!70},
line/.style = {-latex'}
dl/.style = {-latex', dashed}, % <-- added for dashed "join" lines
jdl/.style = {suppress join, join= by dl}% <-- enable dashed "join" lines
]
%nodes
\node [S] (S1) {\textbf{START}};
\node [I] (I1) {$ x $};
\node [I] (I2) {$ n $};
\node [C] (C1) {$ n\geq 0 $};
\node [V] (V1) {$ i\leftarrow 1 $};
\node [C] (C2) {$ i\leq n $};
\node [V] (V2) {$ y=x+i $};
\node [P] (P1) {$ y $};
\node [V,jdl] % <-- this node is connected with previous by dashed arrow
(V3) {$ i\leftarrow i+1 $};
%
\node [S, suppress join, right=of C2] (S2) {\textbf{STOP}};
%arrows
\draw[dl] (V3.west) -- + (-1,0) |- (C2);% <-- solved OP problem
\draw[line] (C1.east) -- + ( 1,0) |- (I2) % <-- solved OP problem
(C2) edge (S2);
\end{tikzpicture}
\end{document}
在 MWE 中使用正确的定位语法和节点样式定义。绘制垂直线留给join
宏来画。
答案2
定义四个辅助节点:
\node [coordinate, right of=I2, node distance=2cm] (I2r) {};
\node [coordinate, right of=C1, node distance=2cm] (C1r) {};
\node [coordinate, left of=C2, node distance=2cm] (C2l) {};
\node [coordinate, left of=V3, node distance=2cm] (V3l) {};
然后,您可以通过向环境添加这些路径命令来绘制缺失的箭头scope
:
\path (C1) -- (C1r) -- (I2r) -- (I2);
\path (V3) -- (V3l) -- (C2l) -- (C2);
命令\<
和\>
真的适用于您的环境吗?我必须改用\leq
和\geq
。
这是完整的代码及其输出。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}
\tikzset
{C/.style={diamond, draw, fill=blue!20, text width=4.5em,
text badly centered, node distance=3cm, inner sep=0pt
},
I/.style={chamfered rectangle, minimum width=1.5cm, minimum height=1cm,
draw,chamfered rectangle corners=north west
},
P/.style={chamfered rectangle, minimum width=1.5cm, minimum height=1cm,
draw,chamfered rectangle corners=south east
},
V/.style={rectangle, draw, minimum width=1.5cm, minimum height=1cm},
S/.style={draw, ellipse,fill=red!70, node distance=3cm, minimum height=2em},
line/.style={draw, -latex'}
}
\begin{tikzpicture}
\node [S] (S1) {\textbf{START}};
\node [I, below of=S1,node distance=1.5cm] (I1) {$ x $};
\node [I, below of=I1,node distance=1.5cm] (I2) {$ n $};
\node [C, below of=I2,node distance=2.1cm] (C1) {$ n\geq 0 $};
\node [V, below of=C1,node distance=2.1cm] (V1) {$ i\leftarrow 1 $};
\node [C, below of=V1,node distance=2.1cm] (C2) {$ i\leq n $};
\node [S, right of=C2,node distance=2.7cm] (S2) {\textbf{STOP}};
\node [V, below of=C2,node distance=2.1cm] (V2) {$ y=x+i $};
\node [P, below of=V2,node distance=1.5cm] (P1) {$ y $};
\node [V, below of=P1,node distance=1.5cm] (V3) {$ i\leftarrow i+1 $};
\node [coordinate, right of=I2, node distance=2cm] (I2r) {};
\node [coordinate, right of=C1, node distance=2cm] (C1r) {};
\node [coordinate, left of=C2, node distance=2cm] (C2l) {};
\node [coordinate, left of=V3, node distance=2cm] (V3l) {};
\begin{scope} [every path/.style=line]
\path (S1) -- (I1);
\path (I1) -- (I2);
\path (I2) -- (C1);
\path (C1) -- (V1);
\path (V1) -- (C2);
\path (C2) -- (V2);
\path (C2) -- (S2);
\path (V2) -- (P1);
\path (P1) -- (V3);
\path (C1) -- (C1r) -- (I2r) -- (I2);
\path (V3) -- (V3l) -- (C2l) -- (C2);
\end{scope}
\end{tikzpicture}
\end{document}