定位节点并计算边坐标

定位节点并计算边坐标

我正在尝试用 tikz 绘制流程图,但遇到了 2 个问题。

第一个是关于 (action5) 的定位。我希望该节点位于 (action4) 左侧和 (test1) 下方。在我的代码中,该节点与 (test1) 不对齐。

第二个问题是关于 (wait1) 中红色边缘的定位。我希望边缘接触 (test1) 和 (action4) 之间的边缘。

这是我的代码:

\documentclass{book}

\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{positioning,shadows,arrows,shapes,shapes.arrows,trees,calc}

\tikzset{
wait/.style={circle, draw=black, text centered, anchor=north, text=black, font=\scriptsize},
end/.style={draw=none, fill=none, text centered, anchor=north}
wait/.style={circle,draw=black,text centered,anchor=north,text=black,font=\scriptsize},
decision/.style={diamond,draw=black,    inner sep=0.18cm,text centered,anchor=north,text=black},
action/.style={ regular polygon,regular polygon sides=4,inner sep=0.2cm,draw=black,text centered,anchor=north,text=black},
foreach/.style={regular polygon,regular polygon sides=3,draw=black,text centered,anchor=north,text=black},
endforeach/.style={ foreach,shape border rotate=180}        
}

\begin{document}

\begin{figure}[h] 
\begin{tikzpicture}[align=center]

        \node (start) [end] {Begin $P_{I2-1}(partial, firstAppNode)$};
        \node (action1) [action,below=0.5 of start,label=0:{Init graph matching algorithm \\ (partial, firstAppNode)}] {};  
        \node (action2) [action, below=1 of action1, label=0:{Compute next partial projection}] {};
        \node (test1) [decision, below=0.5 of action2] {};
        \node (foreach1) [foreach, below right = 0.5 and 4 of test1,label=0:{ForEach $appNeighbour$ of $firstAppNode$}] {};
        \node (foreach2) [foreach, below right = 1 and 1 of foreach1,label=180:{ForEach $neighbour$}] {};
        \node (action3) [action,below=1 of foreach2,label=180:{Send message \\ to neighbour \\ request(projection, ...)}] {};
        \node (wait1) [wait, below=1 of action3] {$\mathcal{W}$};
        \node (endforeach2) [endforeach, below=0.5 of wait1] {};
        \node (endforeach1) [endforeach, below left=0.5 and 1 of endforeach2] {};
        \node (action4) [action, below=0.5 of endforeach1, label=0:{Send message to A \\ response(partial)}] {};
        \node (action5) [action, left=4 of action4, label=0:{Send message to A \\ response(projection)}] {}; %
        \node (action6) [action, left=1 of action5, label=180:{Send message to A \\ response(none)}] {};
        \node (end1) [end, below=0.5 of action4] {End ok};
        \node (end2) [end, below=0.5 of action5] {End ok};
        \node (end3) [end, below=0.5 of action6] {End ok};

        \draw[->] (start)--(action1);
        \draw[->] (action1)--(action2);
        \draw[->] (action2)--(test1);
        \draw[->,red] (test1)--(action5) node[pos=0.04,left] {complete};
        \draw[->] (test1)-|(action6) node[pos=0.15,above] {fail};
        \draw[->] (test1)-|(foreach1) node[pos=0.15,above] {projection};
        \draw[->] (foreach1)|-($(foreach1)!0.5!(foreach2)$)-|(foreach2);
        \draw[->] (foreach2)--(action3);
        \draw[->] (action3)--(wait1);
        \draw[->] (wait1)--(endforeach2) node[pos=0.3,right] {response(none)};
        \coordinate[shift={(0,1)}] (wait1endforeach1) at (endforeach1.north);
        \draw[->] (wait1)--(wait1endforeach1)--(endforeach1) node[pos=0.2,left] {response(partial)};
        \coordinate[shift={(-5,0)}] (wait1action4) at (wait1.west);
        \draw[->,red] (wait1)--(wait1action4) node[pos=0.3,above] {response(complete)};
        \draw[->] (endforeach1)--(action4);
        \draw[->] (action4)--(end1);
        \draw[->] (action5)--(end2);
        \draw[->] (action6)--(end3);

        \coordinate[shift={(-3,0)}] (linkforeach1) at (foreach1.south);
        \draw[dashed] (foreach1) -| (linkforeach1) |- (endforeach1) ;   
        \coordinate[shift={(3,0)}] (linkforeach2) at (foreach2.south);
        \draw[dashed] (foreach2) -| (linkforeach2) |- (endforeach2) ;   

        \coordinate[shift={(5.5,-0.5)}] (foreach2action2) at (endforeach2.south);
        \draw[->] (endforeach2) |- (foreach2action2) |- ($(action1)!0.5!(action2)$) ;   


        \node[above,shift=({0,-0.1cm})] at(start.north) {\textbf{Compute application projection}};
\end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

答案1

第一个问题:

\node (action5) [action, label=0:{Send message to A \\ response(projection)}] at (action4 -| test1){}; %  

第二个问题:

\draw[->,red] (wait1)--($(test1)!(wait1)!(action5)$) node[pos=0.3,above] {response(complete)};

请不要添加无法编译的代码(缺少许多样式和库)。阅读代码不仅非常无聊,而且您得到的答案也未经测试。

相关内容