如何绘制内部有矩形的结构

如何绘制内部有矩形的结构

我希望使用下面的代码绘制如图所示的流程,但是我发现使用坐标定位矩形非常困难。有没有更好的方法可以做到这一点?

在此处输入图片描述

以下是我尝试过的代码:

\documentclass{minimal}
\usepackage{tikz}
\usepackage{pgf}
\usetikzlibrary{arrows,automata} 
\usetikzlibrary{shapes}
\begin{document}


\begin{tikzpicture}
\draw (-4,-4) rectangle (4,4);
\draw (-0.5,0)  rectangle (-3,-3);
\draw (0.5,0)   rectangle (3,-3);
\draw (-1,0)    rectangle (-2,-1);

\end{tikzpicture}

\end{document}

答案1

该答案使用了以下 TikZ 库:

  • shapes.geometric对于diamond形状
  • positioning用于节点的相对定位 ( …=… of …)
  • fit用于将节点拟合到其他节点周围 ( fit=(…)(…) …)
  • calc用于钻石节点的定位

节点与和f位于同一高度。 节点与左侧菱形 ( ) 位于同一高度。bdedia1

右边的大矩形包围ef使用坐标dia1.north -| e进行拟合,使得它与另一个大矩形具有相同的高度。

底部的“加号”菱形是否应水平居中于大矩形之间?

\node[dia cross] at ([yshift=-.5cm]$(left.south east)!.5!(right.south west)$) (dia2) {};

用于放置(注意锚点south eastsouth west)。

代码

\documentclass[tikz]{standalone}
\usetikzlibrary{
    shapes.geometric,
    positioning,
    fit,
    calc
}
\tikzset{
    dia/.style={
        shape=diamond,
        minimum size=2em,
    },
    dia cross/.style={
        dia,
%       path picture={
%           \draw (path picture bounding box.west) -- (path picture bounding box.east)
%                 (path picture bounding box.north) -- (path picture bounding box.south);
%       },
        append after command={
            \pgfextra
                \draw[shorten >=\pgflinewidth,shorten <=\pgflinewidth]
                                   (\tikzlastnode.west) -- (\tikzlastnode.east);
                \draw[shorten >=\pgflinewidth,shorten <=\pgflinewidth]
                                 (\tikzlastnode.north) -- (\tikzlastnode.south);
            \endpgfextra
        }

    }
}
\begin{document}
\begin{tikzpicture}[
    every node/.append style={draw,minimum width=3em,minimum height=1.5em},
    >=latex
]

%%% Left side
%% nodes with text
\node                (a) {a};
\node[below=.5 of a] (b) {b};

\node[right=of a]    (c) {c};
\node[below=.5 of c] (d) {d};

%% rectangles around nodes with text
\node[fit=(a)(b)] (ab) {};
\node[fit=(c)(d)] (cd) {};

%% top diamond
\node[dia] at ([yshift=.5cm]$(ab.north)!.5!(cd.north)$) (dia1) {};

%%% Right side
\node[right=2 of d] (f) {f};
\node[label={[inner sep=0pt,minimum height=2ex]above:h?}] at (f |- dia1) (e) {e};

% fake f to get the same spacing as on the left side (text with draw=gray)
\node[fit=(f),draw=none] (f') {}; 

%%% Big rectangles
\node[inner sep=1em,fit=(ab)(cd)(dia1)] (left) {};
\node[inner sep=1em,fit=(f')(dia1.north -| e)] (right) {};

%%% Diamond below
\node[dia cross] at ([yshift=-.5cm]$(left.south)!.5!(right.south)$) (dia2) {};

%%% Arrows
\foreach \pp/\pf/\pt in {--/a/b,
                         --/c/d,
                         --/e/f,
                         -|/dia1/ab,
                         -|/dia1/cd,
                         |-/left/dia2,
                         |-/right/dia2}
\draw[
    shorten >=\pgflinewidth,
    ->
] (\pf) \pp (\pt);
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

相关内容