Tikz:比较两个节点的坐标以确定下一个节点的坐标

Tikz:比较两个节点的坐标以确定下一个节点的坐标

我目前正在制作一个流程图来描述算法中模块的依赖关系。有几个分支,这些分支在某个时刻重新连接。因为我的算法仍在变化,所以流程图将会更新。

我希望让节点(Target_02)重新连接分支,依赖于预分割节点(Origin)的 x 坐标和每个分支中两个最终节点的 y 坐标,方法是取节点(Operation_01cc)和节点(Target_01a)之间 y 值的最低值。

或者:如何比较两个节点之间的坐标?

在此处输入图片描述

\documentclass[border={5pt 5pt 5pt 5pt}]{standalone}

\usepackage{tikz}

\usetikzlibrary{arrows.meta}
\tikzset{%
>={Latex[width=2mm,length=2mm]},
% Specifications for style of nodes:
base/.style = {rectangle, rounded corners, draw=black,
    minimum width=4cm, minimum height=1cm,
    text centered, font=\sffamily},
Table/.style = {base, bottom color=yellow!70, top color=white},
TableTarget/.style = {base, bottom color=green!75!black!70, top color=white},
AnOperation/.style = {base, bottom color=gray!70, top color=white},
finalTarget/.style = {base, bottom color=blue!70, top color=white, draw=blue},
}
\begin{document}    
% Drawing part, node distance is 1.5 cm and every node
% is prefilled with white backgroun
\begin{tikzpicture}[node distance=1.5cm,
every node/.style={fill=white, font=\sffamily}, align=center, scale=0.8]

\node (Origin)          [Table]{00};                                                        

%%%%%%%%%%%%%%%%%%%%%%%%
%%% PREPPING SOURCE. %%%
%%%%%%%%%%%%%%%%%%%%%%%%

\node (Operation_00b)   [AnOperation, above of=Origin] {};                          
\node(Source_00b)       [Table, left of=Operation_00b, xshift=-3 cm] {};    
\node (Operation_00a)   [AnOperation, above of=Operation_00b] {};   
\node(Source_00a)       [Table, left of=Operation_00a, xshift=-3 cm] {};                                
\node (Source_Table)    [Table, above of=Operation_00a] {};                 

\draw[->]               (Source_Table)              -- (Operation_00a);   
\draw[->]               (Source_00a)    -- (Operation_00a);                         
\draw[->]               (Operation_00a)         -- (Operation_00b);
\draw[->]               (Source_00b)        -- (Operation_00b);                         
\draw[->]               (Operation_00b)         -- (Origin);                                


%%%%%%%%%%%%%%%%%%%
%%% BRANCH 01a. %%%
%%%%%%%%%%%%%%%%%%%

\node (Operation_01aa)  [AnOperation, below of=Origin, xshift=-9cm] {};
\node (Source_01aa)     [Table, left of=Operation_01aa, xshift=-3cm] {};    
\node (Operation_01ab)  [AnOperation, below of=Operation_01aa] {};                                                                      
\node (Source_01ab)     [Table, left of=Operation_01ab, xshift = -3cm] {};  
\node (Operation_00ac)  [AnOperation, below of=Operation_01ab] {};
\node (Source_01ac)     [Table, left of=Operation_00ac, xshift = -3cm]  {};
\node (Target_01a)      [TableTarget, below of=Operation_00ac]      {01ac}; 

\draw[->]               (Source_01aa) -- (Operation_01aa);                                      
\draw[->]               (Origin) -| (Operation_01aa);                   
\draw[->]               (Source_01ab) -- (Operation_01ab);                                      
\draw[->]               (Operation_01aa) -- (Operation_01ab);                                   
\draw[->]               (Source_01ac) -- (Operation_00ac);
\draw[->]               (Operation_01ab) -- (Operation_00ac);
\draw[->]               (Operation_00ac) -- (Target_01a);                               


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% BRANCH 01c. %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\node (Operation_01ca)  [AnOperation, below of=Origin, xshift=9cm]{};
\node (Source_01ca)     [Table,left of=Operation_01ca, xshift=-3cm] {}; 
\node (Target_01ca)     [TableTarget, below of=Operation_01ca]   {}; 
\node (Operation_01cb)  [AnOperation,below of=Target_01ca]    {};   
\node (Target_01cb)     [TableTarget, below of=Operation_01cb]  {}; 
\node(Operation_01cc)   [AnOperation, below of=Target_01cb] {01cc}; 

\draw[->]               (Source_01ca) --  (Operation_01ca);                                     
\draw[->]               (Origin) -| (Operation_01ca);       
\draw[->]               (Operation_01ca) -- (Target_01ca);      
\draw[->]               (Target_01ca) -- (Operation_01cb);      
\draw[->]               (Operation_01cb) -- (Target_01cb);      
\draw[->]               (Target_01cb) -- (Operation_01cc);                              

%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% RECOMBINE BRANCHES %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%  

\node (Target_02)   [TableTarget, below of=Operation_01cc, xshift=-9cm] {};

\draw[->]               (Operation_01cc) |- (Target_02);        
\draw[->]               (Target_01a) |- (Target_02);                            

%%%%%%%%%%%%%%%%%%%%%%%%%
%%% FURTHER OPERATION %%%
%%%%%%%%%%%%%%%%%%%%%%%%%

\node(Operation_03a)    [AnOperation, below of=Target_02] {};                                       
\node(Source_03a)       [Table, left of=Operation_03a, xshift=-3cm] {};                             
\node(Operation_03b)    [AnOperation, below of=Operation_03a] {};       
\node(Target_03b)       [TableTarget, below of=Operation_03b] {};
\node(future)           [TableTarget, below of=Target_03b] {};

\draw[->]               (Operation_03b) -- (Target_03b);  
\draw[->]               (Operation_03a) -- (Operation_03b); 
\draw[->]               (Source_03a) -- (Operation_03a);    
\draw[->]               (Target_02) -- (Operation_03a); 
\draw[->]               (Target_03b) -- (future);   

%%%%%%%%%%%%%%%%%%%%%
%% THE FINAL TABLE %%
%%%%%%%%%%%%%%%%%%%%%

\node(finalTarget)  [finalTarget, below of=future]  {};

\draw[->]           (future) -- (finalTarget);  
\draw[->]           (Origin)  -- (Target_02);                       

\end{tikzpicture}

\end{document}

答案1

您可以提取坐标,但使用fit库围绕相关操作和目标节点创建节点可能更容易。

我还推荐该positioning库。below of=等已被弃用。

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,fit,positioning}
\tikzset{%
  >={Latex[width=2mm,length=2mm]},
  base/.style = {rectangle, rounded corners, draw=black, minimum width=4cm, minimum height=1cm, text centered, font=\sffamily},
  Table/.style = {base, bottom color=yellow!70, top color=white},
  TableTarget/.style = {base, bottom color=green!75!black!70, top color=white},
  AnOperation/.style = {base, bottom color=gray!70, top color=white},
  finalTarget/.style = {base, bottom color=blue!70, top color=white, draw=blue},
}
\begin{document}
\begin{tikzpicture}[->, node distance=1.5cm, every node/.style={fill=white, font=\sffamily}, align=center, scale=0.8]

  \node (Origin)          [Table]{00};

  \node (Operation_00b)   [AnOperation, above of=Origin] {};
  \node(Source_00b)       [Table, left of=Operation_00b, xshift=-3 cm] {};
  \node (Operation_00a)   [AnOperation, above of=Operation_00b] {};
  \node(Source_00a)       [Table, left of=Operation_00a, xshift=-3 cm] {};
  \node (Source_Table)    [Table, above of=Operation_00a] {};

  \draw               (Source_Table)              -- (Operation_00a);
  \draw               (Source_00a)    -- (Operation_00a);
  \draw               (Operation_00a)         -- (Operation_00b);
  \draw               (Source_00b)        -- (Operation_00b);
  \draw               (Operation_00b)         -- (Origin);

  \node (Operation_01aa)  [AnOperation, below of=Origin, xshift=-9cm] {};
  \node (Source_01aa)     [Table, left of=Operation_01aa, xshift=-3cm] {};
  \node (Operation_01ab)  [AnOperation, below of=Operation_01aa] {};
  \node (Source_01ab)     [Table, left of=Operation_01ab, xshift = -3cm] {};
  \node (Operation_00ac)  [AnOperation, below of=Operation_01ab] {};
  \node (Source_01ac)     [Table, left of=Operation_00ac, xshift = -3cm]  {};
  \node (Target_01a)      [TableTarget, below of=Operation_00ac]      {01ac};

  \draw               (Source_01aa) -- (Operation_01aa);
  \draw               (Origin) -| (Operation_01aa);
  \draw               (Source_01ab) -- (Operation_01ab);
  \draw               (Operation_01aa) -- (Operation_01ab);
  \draw               (Source_01ac) -- (Operation_00ac);
  \draw               (Operation_01ab) -- (Operation_00ac);
  \draw               (Operation_00ac) -- (Target_01a);

  \node (Operation_01ca)  [AnOperation, below of=Origin, xshift=9cm]{};
  \node (Source_01ca)     [Table,left of=Operation_01ca, xshift=-3cm] {};
  \node (Target_01ca)     [TableTarget, below of=Operation_01ca]   {};
  \node (Operation_01cb)  [AnOperation,below of=Target_01ca]    {};
  \node (Target_01cb)     [TableTarget, below of=Operation_01cb]  {};
  \node(Operation_01cc)   [AnOperation, below of=Target_01cb] {01cc};

  \draw               (Source_01ca) --  (Operation_01ca);
  \draw               (Origin) -| (Operation_01ca);
  \draw               (Operation_01ca) -- (Target_01ca);
  \draw               (Target_01ca) -- (Operation_01cb);
  \draw               (Operation_01cb) -- (Target_01cb);
  \draw               (Target_01cb) -- (Operation_01cc);

  \node (f) [inner sep=0pt, draw=none, fill=none, fit=(Operation_01cc) (Target_01a)] {};
  \node (Target_02)   [TableTarget, below=of Origin |- f.south] {};

  \draw               (Operation_01cc) |- (Target_02);
  \draw               (Target_01a) |- (Target_02);

  \node(Operation_03a)    [AnOperation, below of=Target_02] {};
  \node(Source_03a)       [Table, left of=Operation_03a, xshift=-3cm] {};
  \node(Operation_03b)    [AnOperation, below of=Operation_03a] {};
  \node(Target_03b)       [TableTarget, below of=Operation_03b] {};
  \node(future)           [TableTarget, below of=Target_03b] {};

  \draw               (Operation_03b) -- (Target_03b);
  \draw               (Operation_03a) -- (Operation_03b);
  \draw               (Source_03a) -- (Operation_03a);
  \draw               (Target_02) -- (Operation_03a);
  \draw               (Target_03b) -- (future);

  \node(finalTarget)  [finalTarget, below of=future]  {};

  \draw           (future) -- (finalTarget);
  \draw           (Origin)  -- (Target_02);
\end{tikzpicture}
\end{document}

一个合适的解决方案,可能

相关内容