TikZ 中的连接节点

TikZ 中的连接节点

我正在尝试绘制附图中的图形,但不确定如何连接其他节点之间的线。 在此处输入图片描述

我的输出: 在此处输入图片描述

\begin{tikzpicture}[>=stealth]
% Leader Node
\node (leader) [circle, draw=black!80, fill=red!40, very thick, minimum size=7mm] {\textbf{0}};
%
\node (Agent1) [circle, above right= 0.5cm and 1.5cm of leader, draw=black!80, fill=yellow!40, very thick, minimum size=7mm] {\textbf{1}};
  \draw[very thick,->, right] (leader) edge (Agent1);
%
\node (Agent2) [circle, below right= 0.5cm and 1.5cm of leader, draw=black!80, fill=yellow!40, very thick, minimum size=7mm] {\textbf{2}};
  \draw[very thick,->, right] (leader) edge (Agent2);
%
\node (Agent3) [circle, right= 1.5cm of Agent2, draw=black!80, fill=yellow!40, very thick, minimum size=7mm] {\textbf{3}};

%
\node (Agent4) [circle, right= 1.5cm of Agent1, draw=black!80, fill=yellow!40, very thick, minimum size=7mm] {\textbf{4}};

\end{tikzpicture}

答案1

使用元素的重复特征来定义样式非常有用。(本例中为节点和箭头)。节省输入和错误;并使代码更易于理解。

\documentclass[12pt,a4paper]{article}

\usepackage{tikz}
\usetikzlibrary{positioning}% <<<<<<<<<<<

\begin{document}
    \begin{tikzpicture}[>=stealth, 
        font= \bfseries,
         ynode/.style={circle, draw=black!80, fill=yellow!40, very thick, minimum size=7mm},
         barrow/.style={very thick,->}
         ]
        % Leader Node
        \node (leader) [ynode, fill=red!40] {0};
        \node (Agent1) [ynode, above right= 0.5cm and 1.5cm of leader] {1};
        \node (Agent2) [ynode, below right= 0.5cm and 1.5cm of leader] {2};
        \node (Agent3) [ynode, right= 1.5cm of Agent2] {3};
        \node (Agent4) [ynode, right= 1.5cm of Agent1] {4};
        % arrows x 6
        \draw[barrow] (leader) edge (Agent1);
        \draw[barrow] (leader) edge (Agent2);
        \draw[barrow] (Agent1) edge (Agent4);
        \draw[barrow] (Agent2) edge (Agent3);
        \draw[barrow] (Agent4) edge (Agent2);
        \draw[barrow] (Agent3) edge (Agent4);
    
    \end{tikzpicture}
\end{document}

A

答案2

正如 Jasper Habicht 评论的那样:

只需在末尾添加四个 \draw 宏:\draw[very thick, ->] (Agent1) edge (Agent4); \draw[very thick, ->] (Agent4) edge (Agent2); \draw[very thick, ->] (Agent2) edge (Agent3); \draw[very thick, ->] (Agent3) edge (Agent4);。

而且我很确定箭头类型是“-latex”,所以我改变了它。

\documentclass[american]{article}    
\begin{tikzpicture}[>=latex]
    % Leader Node
    \node (leader) [circle, draw=black!80, fill=red!40, very thick, minimum size=7mm] {\textbf{0}};
    %
    \node (Agent1) [circle, above right= 0.5cm and 1.5cm of leader, draw=black!80, fill=yellow!40, very thick, minimum size=7mm] {\textbf{1}};
      \draw[very thick,->, right] (leader) edge (Agent1);
    %
    \node (Agent2) [circle, below right= 0.5cm and 1.5cm of leader, draw=black!80, fill=yellow!40, very thick, minimum size=7mm] {\textbf{2}};
      \draw[very thick,->, right] (leader) edge (Agent2);
    %
    \node (Agent3) [circle, right= 1.5cm of Agent2, draw=black!80, fill=yellow!40, very thick, minimum size=7mm] {\textbf{3}};
    
    %
    \node (Agent4) [circle, right= 1.5cm of Agent1, draw=black!80, fill=yellow!40, very thick, minimum size=7mm] {\textbf{4}};
    \draw[very thick, ->] (Agent1) edge (Agent4); 
    \draw[very thick, ->] (Agent4) edge (Agent2); 
    \draw[very thick, ->] (Agent2) edge (Agent3); 
    \draw[very thick, ->] (Agent3) edge (Agent4);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

还有一个可能的解决方案:

  • 绘制的都是节点之间的箭头
  • 节点和边的样式定义在图片前言中
  • 节点之间的距离定义为node distance
  • 使用上述代码可以使您的图像更加简洁和简单。
\documentclass[tikz, margin=3.14159mm]{standalone}
\usetikzlibrary{arrows.meta,
                positioning}

\begin{document}
    \begin{tikzpicture}[
node distance = 5mm and 15mm,   % <---
     C/.style = {circle, draw=black!80, very thick,
                 fill=#1, minimum size=7mm, font=\bfseries}, % <---
     C/.default = yellow!40,    % <---
every edge/.append style = {draw, semithick, -Stealth} % <---
                        ]
% Leader Node
\node (leader) [C=red!40] {0};
\node (Agent1) [C, above right=of leader] {1};
\node (Agent2) [C, below right=of leader] {2};
\node (Agent3) [C, right=of Agent2] {3};
\node (Agent4) [C, right=of Agent1] {4};
%
\draw   (leader) edge (Agent1)
        (leader) edge (Agent2)
        (Agent1) edge (Agent4)
        (Agent2) edge (Agent3)
        (Agent4) edge (Agent2)
        (Agent4) edge (Agent3);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容