在 TikZ 中强制节点之间的水平边缘

在 TikZ 中强制节点之间的水平边缘

在此代码中:

\usetikzlibrary{positioning, fit, calc, shapes, arrows}
\renewcommand{\figurename}{Figure}
\begin{figure}[!htb]
    \centering
    \begin{tikzpicture}
        [title/.style={font=\fontsize{18}{18}\color{black!45}},
        server/.style={rectangle, draw, fill=blue!23, rounded corners, minimum height=9em},
        client/.style={rectangle, draw, fill=green!23, rounded corners, minimum height=2em},
        dot/.style={circle, fill=black, minimum size=2pt, inner sep=0pt, outer sep=2pt},
        link/.style={latex-latex}]
        % Place nodes
        \node [server, outer sep=8pt, align=center] (s1) at (5,8.3) {S1};
        \node [server, outer sep=8pt, align=center] (s2) at (7.65,8.3) {S2};
        \node [server, outer sep=8pt] (s3) at (10.3,8.3) {S3};
        \node [draw=black!50, fit={(s1) (s2) (s3)}] (back) {};
        \node [client] (c1) at (0,9.5) {C1};
        \node [client] (c2) at ($(c1) + (270:1.15)$) {C2};
        \node [client] (c3) at ($(c2) + (270:1.15)$) {C3};
        \node [dot] (c4) at ($(c3) + (270:1)$) {};
        \node [dot] (c5) at ($(c4) + (270:0.5)$) {};
        \node [dot] (c6) at ($(c5) + (270:0.5)$) {};
        \node [draw=black!50, fit={($(c1.north)+(0, 8pt)$) (c2) (c3) (c4) (c5) ($(c6.south)+(0, -7pt)$)}, inner sep=5.75pt] (front) {};
        % Place edges
    \end{tikzpicture}
    \caption{Schema}
\end{figure}

我想要一个单独的水平的C1边将、C2和分别与包含、和C3的矩形节点连接起来,即上面代码中的节点。S1S2S3back

如果我使用\path,边缘不是水平的,因为的坐标C1C2并且C3节点与节点不对齐back

我该怎么做?

答案1

你可以使用正交坐标系语法来实现这一点。如果你说

\draw (c1) -- (c1-|back.west)

你将得到一条从节点c1到 y 坐标为c1、 x 坐标为 的点的线back.west

\documentclass[border=5mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning, fit, calc, shapes, arrows}

\begin{document}
    \begin{tikzpicture}
        [title/.style={font=\fontsize{18}{18}\color{black!45}},
        server/.style={rectangle, draw, fill=blue!23, rounded corners, minimum height=9em},
        client/.style={rectangle, draw, fill=green!23, rounded corners, minimum height=2em},
        dot/.style={circle, fill=black, minimum size=2pt, inner sep=0pt, outer sep=2pt},
        link/.style={latex-latex}]
        % Place nodes
        \node [server, outer sep=8pt, align=center] (s1) at (5,8.3) {S1};
        \node [server, outer sep=8pt, align=center] (s2) at (7.65,8.3) {S2};
        \node [server, outer sep=8pt] (s3) at (10.3,8.3) {S3};
        \node [draw=black!50, fit={(s1) (s2) (s3)}] (back) {};
        \node [client] (c1) at (0,9.5) {C1};
        \node [client] (c2) at ($(c1) + (270:1.15)$) {C2};
        \node [client] (c3) at ($(c2) + (270:1.15)$) {C3};
        \node [dot] (c4) at ($(c3) + (270:1)$) {};
        \node [dot] (c5) at ($(c4) + (270:0.5)$) {};
        \node [dot] (c6) at ($(c5) + (270:0.5)$) {};
        \node [draw=red!50, fit={($(c1.north)+(0, 8pt)$) (c2) (c3) (c4) (c5) ($(c6.south)+(0, -7pt)$)}, inner sep=5.75pt] (front) {};
        \draw (c1) -- (c1-|back.west)
            (c2) -- (c2-|back.west)
            (c3) -- (c3-|back.west);
        % Place edges
    \end{tikzpicture}
\end{document}

相关内容