如何在 tikz 中绘制这个简单的图形?

如何在 tikz 中绘制这个简单的图形?

在此处输入图片描述

这与无线传感器网络有关。有人能帮忙吗?

答案1

作为起点:

在此处输入图片描述

在此处输入图片描述

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

\begin{document}
    \begin{tikzpicture}[
    node distance = 0pt,
box/.style = {draw, fill=#1, align=center,
              text width=5em, text height=2ex, text depth=0.25ex,
              outer sep=0pt}
                        ]
\draw[thick] (0,0) node (n1) [above left] {Sender} -- ++ (7,0);
\node (n2) [box=green,above] at (3.5,0) {packet};
%
\draw[thick] (0,-1) node (n3) [above left] {Receiver} -- ++ (7,0);
\node (n4) [box=green,above] at (3.5,-1) {packet};
\node (n5) [box=red, left=of n4]    {idle};;
\node (n6) [box=red, right=of n4]   {idle};;
   \end{tikzpicture}

% second example
    \begin{tikzpicture}[
    node distance = 0pt,
box/.style = {draw, fill=#1, align=center,
              text width=5em, text height=2ex, text depth=0.25ex,
              outer sep=0pt}
                        ]
\draw[thick] (0,0) node (n1) [above left] {Sender A} -- ++ (7,0);
\node (n2) [box=green,above] at (2,0) {packet};
%
\draw[thick] (0,-1) node (n3) [above left] {Sender B} -- ++ (7,0);
\node (n4) [box=green,above] at (3,-1) {packet};
%
\draw[thick] (0,-2) node (n5) [above left] {Receiver} -- ++ (7,0);
    \scoped[text=white]
{
\node (n6) [box=green, above] at (2,-2) {packet};
\node (n4) [box=red,above]    at (3,-2) {garbage};
}
\end{document}

编辑: 为了练习,添加了一个例子。

相关内容