用线连接两个矩形

用线连接两个矩形

我用这个方法画了两个矩形:

\draw[fill=blue](0,0) rectangle (1,1) node[midway,align=center]{my text};

我不知道如何使用锚点连接这些矩形。我必须使用\node方法绘制它们吗?这个想法来自这里

我需要一种方法来获取矩形的两个角,因为我将使用这些信息来绘制新的形状。

答案1

还有一个答案...

正如@Ignasi 在他的回答中所说,只有节点才有锚点。您可以使用宽度和高度选项预定义节点形状,这些选项在使用它时确定。其中的文本居中,它们的起始坐标可以像上面的 MWE 中一样在 tikzlibrary 的帮助下定义`定位:

\documentclass[border=3mm,tikz]{standalone}
    \usetikzlibrary{positioning}

\begin{document}
    \begin{tikzpicture}[
    node distance=0mm,
     box/.style args = {#1/#2}{shape=rectangle,
            text width=#1mm, minimum height=#2mm,
            draw, thick, inner sep=0pt, outer sep=0pt, 
            align=center, text=black}
                    ]
\draw[thin, gray!25] (0,0) grid (8,8);
%
 \node[box=20/10,blue,above right=of {(0,1)}] (a) {my text};%
 \node[box=30/40,red, above right=of {(5,4)}] (b) {my very very very very long text}; %
%
\draw[thick, red] (a) -- (b);
\draw[thick, blue] (a) |- (b);
\draw[thick, green] (a.north east) -- (b.south west);
%
\draw[<-] (b.south west) -- + (0,-1) node[below] {coordinate (5,4)};
\draw[very thin,densely dashed] (a.center) -- (b.center);
    \end{tikzpicture}
\end{document}

或者直接:

\documentclass[border=3mm,tikz]{standalone}

\begin{document}
    \begin{tikzpicture}[
    node distance=0mm,
     box/.style args = {#1/#2}{shape=rectangle,
            text width=#1mm, minimum height=#2mm,
            draw, thick, inner sep=0pt, outer sep=0pt, 
            above right, align=center, text=black}
                    ]
\draw[thin, gray!25] (0,0) grid (8,8);
%
 \node[box=20/10,blue] (a) at (0,1) {my text};%
 \node[box=30/40,red ] (b) at (5,4) {my very very very very long text}; %
%
\draw[thick, red] (a) -- (b);
\draw[thick, blue] (a) |- (b);
\draw[thick, green] (a.north east) -- (b.south west);
%
\draw[<-] (b.south west) -- + (0,-1) node[below] {coordinate (5,4)};
\draw[very thin,densely dashed] (a.center) -- (b.center);
    \end{tikzpicture}
\end{document}

在两种情况下我们都得到相同的结果:

在此处输入图片描述

答案2

除非您知道要连接的特定坐标,否则无法连接两个矩形。矩形没有anchorsnodes但有。

虽然我确信这个问题已经在这里得到解答了,但是您可以使用库绘制一个固定大小的node矩形。作为节点,您可以使用锚点连接这些特定的矩形。anchorsfit

一个例子:

\documentclass[tikz,border=2mm]{standalone}

\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}

     \draw[thin, gray] (0,0) grid (8,8);

     \node[thick, blue, fit={(0,1) (3,3)}, inner sep=0pt, draw] (a) {};
     \node[thick, red, fit={(5,4) (8,8)}, inner sep=0pt, draw] (b) {};

    \draw[thick, red] (a) -- (b);
    \draw[thick, blue] (a) |- (b);
    \draw[thick, green,] (a.east) to[bend right] (b.south);

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

您可以连接矩形中的节点on background layer,这样它们看起来就像您连接了矩形本身一样。这里的限制是您不能添加箭头等。

\documentclass[tikz, border = 2pt]{standalone}
\usetikzlibrary{backgrounds}
\begin{document}

\begin{tikzpicture}
\draw[fill=blue!20](0,0) rectangle (2,2) node[midway,align=center](m1){text 1};
\draw[fill=blue!20](4,0) rectangle (6,2) node[midway,align=center](m2){text 2};
\draw[fill=blue!20](2,3) rectangle (4,5) node[midway,align=center](m3){text 3};

\begin{scope}[on background layer]
\draw (m1) -- (m2) (m1) -- (m3);
\end{scope}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案4

我不知道如何使用或定义锚点长方形函数如本答案中所述。因此,我将使用节点作为示例,如下所示

\documentclass[tikz, border = 2pt]{standalone}

     \begin{document}
\begin{tikzpicture}
  % use a node, fill it blue, optional set the border color
  % the distance of the boxed need to be set in this formation with right of
  \node[fill=blue, draw=black, node distance = 2cm] (first) {my text};
  \node[fill=blue, draw=black, right of = first, node distance = 2cm,] (second) {my text};

  % there are different anchors present
  \draw[->] (first) -- (second);
  \draw[red, ->] (first.north east) -- (second.north west);
\end{tikzpicture}
    \end{document}

一个缺点是您不能轻松地限制盒子的尺寸。

相关内容