在流程图节点周围绘制不规则多边形

在流程图节点周围绘制不规则多边形

能否请教一下如何围绕流程图节点绘制不规则多边形。我看到了一种手动方式使用坐标系绘制多边形但这对于流程图来说有点不切实际。

我想要如图所示的 6 边多边形。有没有不用手动输入坐标的方法?

这是我目前的代码:

\documentclass [12pt]{article}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=3cm]{geometry}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, 
arrows, fit,
positioning,
backgrounds}
% \usetikzlibrary{shapes.multipart}



\tikzstyle{startstop} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm,text centered, text width=3cm, draw=black]

\tikzstyle{io} = [trapezium, trapezium left angle=70, trapezium right angle=110, minimum width=3cm, minimum height=1cm, text centered,text width=5cm, draw=black]



\tikzstyle{process} = [rectangle, minimum width=3cm, minimum height=1cm, text centered, text width=5cm, draw=black, fill=none]

\tikzstyle{decision} = [diamond, minimum width=3cm, minimum height=1cm, text centered,text width=3cm, draw=black]

\tikzstyle{arrow} = [thick,->,>=stealth]


\begin{document}


\begin{figure} 
 %  \resizebox{<horizontal size>}{<vertical size>}{%        
\centering
\resizebox{18 cm}{!}{%


\begin{tikzpicture}[ >=latex,auto]


% all nodes
\node (start) [startstop] {Start};
\node (in1) [io, below = of start] {Preprocessor Input: \\ A \\ B \\ C \\ D};

\node (pro1) [process, below  = of in1] {Process 1};

\node (pro2) [process, below = of  pro1] {Process 2};

\node (pro3)[process, below = of pro2] {Calculate: \\  From Variables A and B};

\node (dec1) [decision, below = of pro3] {X or\\ Y?};

\node (pro4)[process, below = of dec1, xshift = 5 cm] {Apply Y loads: \\  Apply $\lambda$ function  };

\node (pro4a)[process, below = of dec1, xshift = -5 cm] {Apply X loads:\\ Apply $\gamma$ function};

\node (pro5) [process, below =  of pro4] {Calculate: \\  U function   };

\node (pro5a) [process, below =  of pro4a] {Calculate: \\  U$_o$ function  };

\node (dec2) [decision, below = of pro5] {No. of desired \\ cycles reached?};

\node (dec2a) [decision, below = of pro5a] {No. of desired \\ cycles reached?};

\node (pro6) [process, below =of dec2a, xshift = 5cm, yshift = -1cm ] {Post-process in :  \\ Python};

\node (pro7)[process, left = of  dec1, xshift=19cm] {Reanalyse \\ Type 1};

\node (pro7a)[process, left = of  dec1, xshift=-7cm] {Reanalyse \\ Type 2};

\node (stop) [startstop,  below =of pro6] {End};

 
 \scoped[on background layer] {\node[regular polygon, draw, regular polygon sides = 4, dashed, fit=(pro4) (pro5)(dec2)(pro7) , fill = olive!25] {};} % using regular polygon
 
\node[left = of dec2, xshift = -10 cm, yshift =  5 cm, fill = olive!80]  {Type 2 System}; 

 \scoped[on background layer] {\node[draw, dashed, fit=(pro4a) (pro5a)(dec2a)(pro7a), fill = olive!25] {};} %using just draw command 
 
\node[right = of dec2a, xshift = 10 cm, yshift =  5 cm, fill = olive!80]  {Type 1 System}; 

%arrows 

\draw [arrow] (start) -- (in1);
\draw [arrow] (in1) -- (pro1);
\draw [arrow] (pro1) -- (pro2);
\draw [arrow] (pro2) -- (pro3);
\draw [arrow] (pro3) -- (dec1);
\draw [arrow] (pro4) -- (pro5);
\draw [arrow] (pro6)--(stop);
\draw [arrow]  (pro7)  |-  (pro1);
\draw [arrow] (pro7a) |- (pro1);
\draw [arrow] (pro4a) -- (pro5a);
\draw [arrow] (pro5a) -- (dec2a);
\draw [arrow] (pro5) -- (dec2);
\draw [arrow] (dec2) -| node[anchor=west] {No } (pro7);
\draw [arrow] (dec2)|- node[anchor=north] {\qquad Yes} (pro6);
\draw [arrow] (dec1) -| node[anchor=east] {X} (pro4a);
\draw [arrow] (dec1) -| node[anchor=west] {Y} (pro4);
\draw [arrow] (dec2a) -| node[anchor=east] {No } (pro7a);
\draw [arrow] (dec2a)|- node[anchor=north] {\qquad Yes} (pro6);

\end{tikzpicture}
}
 
        \caption{Caption for flowchart }
        \label{flowchart}
      
\end{figure}
\end{document}

在此处输入图片描述

答案1

该过程类似于拟合。首先,确定角点,使用直角连接它们,然后添加偏移量。我将标签放在局部边界框的中心(或多或少)。

\documentclass [12pt]{文章}

\usepackage[left=2cm,right=2cm,top=2cm,bottom=3cm]{geometry}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, 
arrows, fit, calc,
positioning,
backgrounds}
% \usetikzlibrary{shapes.multipart}



\tikzstyle{startstop} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm,text centered, text width=3cm, draw=black]

\tikzstyle{io} = [trapezium, trapezium left angle=70, trapezium right angle=110, minimum width=3cm, minimum height=1cm, text centered,text width=5cm, draw=black]



\tikzstyle{process} = [rectangle, minimum width=3cm, minimum height=1cm, text centered, text width=5cm, draw=black, fill=none]

\tikzstyle{decision} = [diamond, minimum width=3cm, minimum height=1cm, text centered,text width=3cm, draw=black]

\tikzstyle{arrow} = [thick,->,>=stealth]


\begin{document}


\begin{figure} 
 %  \resizebox{<horizontal size>}{<vertical size>}{%        
\centering
\resizebox{18 cm}{!}{%
\begin{tikzpicture}[ >=latex,auto]

% all nodes
\node (start) [startstop] {Start};
\node (in1) [io, below = of start] {Preprocessor Input: \\ A \\ B \\ C \\ D};

\node (pro1) [process, below  = of in1] {Process 1};

\node (pro2) [process, below = of  pro1] {Process 2};

\node (pro3)[process, below = of pro2] {Calculate: \\  From Variables A and B};

\node (dec1) [decision, below = of pro3] {X or\\ Y?};

\node (pro4)[process, below = of dec1, xshift = 5 cm] {Apply Y loads: \\  Apply $\lambda$ function  };

\node (pro4a)[process, below = of dec1, xshift = -5 cm] {Apply X loads:\\ Apply $\gamma$ function};

\node (pro5) [process, below =  of pro4] {Calculate: \\  U function   };

\node (pro5a) [process, below =  of pro4a] {Calculate: \\  U$_o$ function  };

\node (dec2) [decision, below = of pro5] {No. of desired \\ cycles reached?};

\node (dec2a) [decision, below = of pro5a] {No. of desired \\ cycles reached?};

\node (pro6) [process, below =of dec2a, xshift = 5cm, yshift = -1cm ] {Post-process in :  \\ Python};

\node (pro7)[process, left = of  dec1, xshift=19cm] {Reanalyse \\ Type 1};

\node (pro7a)[process, left = of  dec1, xshift=-7cm] {Reanalyse \\ Type 2};

\node (stop) [startstop,  below =of pro6] {End};
 
\begin{scope}[on background layer, local bounding box=T1] 
  \path[draw, dashed, fill = olive!25]
    ($(pro4.north west)+(-1em,1em)$) |- ($(dec2.south)+(0,-1em)$) -| ($(pro7.north east)+(1em,1em)$)
    -- ($(pro7.north west)+(-1em,1em)$) |- cycle;
\end{scope}
 
\node[xshift=2em, fill = olive!80] at (T1.center) {Type 1 System}; 

\begin{scope}[on background layer, local bounding box=T2] 
  \path[draw, dashed, fill = olive!25]
    ($(pro7a.north west)+(-1em,1em)$) |-  ($(dec2a.south)+(0,-1em)$) -| ($(pro4a.north east)+(1em,1em)$)
    -| ($(pro7a.north east)+(1em,1em)$) -- cycle;
\end{scope}

\node[xshift=-2em, fill = olive!80] at (T2.center) {Type 2 System}; 

%arrows 

\draw [arrow] (start) -- (in1);
\draw [arrow] (in1) -- (pro1);
\draw [arrow] (pro1) -- (pro2);
\draw [arrow] (pro2) -- (pro3);
\draw [arrow] (pro3) -- (dec1);
\draw [arrow] (pro4) -- (pro5);
\draw [arrow] (pro6)--(stop);
\draw [arrow]  (pro7)  |-  (pro1);
\draw [arrow] (pro7a) |- (pro1);
\draw [arrow] (pro4a) -- (pro5a);
\draw [arrow] (pro5a) -- (dec2a);
\draw [arrow] (pro5) -- (dec2);
\draw [arrow] (dec2) -| node[anchor=west] {No } (pro7);
\draw [arrow] (dec2)|- node[anchor=north] {\qquad Yes} (pro6);
\draw [arrow] (dec1) -| node[anchor=east] {X} (pro4a);
\draw [arrow] (dec1) -| node[anchor=west] {Y} (pro4);
\draw [arrow] (dec2a) -| node[anchor=east] {No } (pro7a);
\draw [arrow] (dec2a)|- node[anchor=north] {\qquad Yes} (pro6);

\end{tikzpicture}
}% resizebox
        \caption{Caption for flowchart }
        \label{flowchart}
      
\end{figure}
\end{document}

演示

答案2

一个可能的解决方案是使用chains库重写您的图表代码来定位节点并用宏绘制大部分连接join,使用最近的语法定义节点样式并重新排列节点位置,以便您可以使用fit (因为您询问如何避免为它们确定坐标)不需要确定不规则形状坐标的库。

通过上述更改,生成的代码与您的代码相比更加简洁:

\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                backgrounds,
                chains,
                fit,
                positioning,
                shapes.geometric}
\makeatletter
\tikzset{FlowChart/.style={
suspend join/.code = {\def\tikz@after@path{}},
startstop/.style = {rectangle, draw, fill=red!30,
                    minimum width=3cm, minimum height=1cm},
  process/.style = {rectangle, rounded corners, draw, fill=orange!30,
                    text width=5cm, minimum height=1cm, align=center},
 decision/.style = {diamond, aspect=1.3, draw, fill=green!30,
                    minimum width=3cm, minimum height=1cm, align=center},
       io/.style = {trapezium, trapezium stretches body,
                    trapezium left angle=70, trapezium right angle=110,
                    draw, fill=blue!30,
                    minimum width=3cm, minimum height=1cm,
                    text width =\pgfkeysvalueof{/pgf/minimum width}-2*\pgfkeysvalueof{/pgf/inner xsep},
                    align=center,
                    },
    arrow/.style = {thick,-Triangle}
                        }   
    }
\makeatother

\begin{document}
    \begin{tikzpicture}[FlowChart,
node distance = 6mm and 12mm,
  start chain = A going below,
   FIT/.style = {draw, dashed, fill = olive!25, 
                 inner sep=2mm, fit=#1}
                        ]
% nodes, 
% top branch
    \begin{scope}[nodes={on chain, join=by arrow}] 
\node   [startstop] {Start};        % A-1
\node   [io]        {Preprocessor Input:\\ A\\ B\\ C\\ D};
\node   [process]   {Process 1};    %  A-3
\node   [process]   {Process 2};
\node   [process]   {Calculate:\\  From Variables A and B};
\node   [decision]  {X or Y?};      % A-6
% left branch    
\node   [process,   % A-7
         suspend join,
         below left= of A-6] {Apply X loads:\\ Apply $\gamma$ function};
\node   [process] {Calculate: \\  U$_o$ function}; 
\node   [decision]  {No. of desired \\ cycles reached?};    % A-9
% right branch
\node   [process,   % A-10
         suspend join,
         below right=of A-6] {Apply Y loads:\\  Apply $\lambda$ function  }; %A-9
\node   [process]   {Calculate: \\  U function};
\node   [decision] {No. of desired \\ cycles reached?};
\node   [process,   % A-13 
         suspend join,
         below=of A-9.south -| A-1] {Post-process in:  \\ Python};
\node   [startstop] {End};
    \end{scope}
%
\node (L)   [process,
             left =of A-6 -| A-7] {Reanalyse\\ Type 2};
\node (R)   [process, right=of A-6 -| A-10] {Reanalyse \\ Type 1};
% fit
\scoped[on background layer]
    \node[FIT=(R) (A-11) (A-12),
          label=\textbf{Type 1 System}] {};
\scoped[on background layer] 
    \node[FIT=(L) (A-8) (A-9),
          label=\textbf{Type 2 System}] {};
% connections not considered in join instruction
\draw [arrow] (A-6) -| node[above] {X} (A-7);
\draw [arrow] (A-6) -| node[above] {Y} (A-10);
%
\draw [arrow] (A-9) -| node[below] {Yes} (L);
\draw [arrow] (L)   |- (A-3);
%
\draw [arrow] (A-12) -| node[below] {No}  (R);
\draw [arrow] (R)    |- (A-3);
%
\draw [arrow] (A-9)  |- node[below] {Yes} (A-13);
\draw [arrow] (A-12) |- node[below] {Yes} (A-13);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

如果您不喜欢有彩色节点(我从一些旧答案中获取的节点定义),请从定义的样式中删除它们的定义。

相关内容