我有六条线从一个点发出,或者六条线汇聚到一个点。有没有简单的方法可以在 TikZ 中编写一行代码?
基本上,我有这段代码,我想减少行数。我知道我可以使用 node 的选项
\documentclass{article}
\usepackage{pgfplots,tikz}
\usetikzlibrary{
positioning,
snakes,
shapes,
shadows
}
\begin{document}
\begin{tikzpicture}
\draw[fill=green!80] (0,0) circle (1);
\draw[fill=black](0,0)circle(0.5ex) node[right]{Access Point};
\draw[fill=blue!50] (1,3)--+(300:5) arc (300:330:5)--cycle;
\draw(3,1.5) node{Main Beam};
\draw[fill=black](1,3) circle(0.5ex) node[above]{Base Station};
\node[draw,star,fill=black,scale=0.3] at (4,0){};
\draw(4,0) node[above] {Cell User};
\draw(55:0.5) node[rectangle,fill=black,scale=0.5](a){};
\draw(120:0.8) node[rectangle,fill=black,scale=0.5](b){};
\draw(210:0.7) node[rectangle,fill=black,scale=0.5](c){};
\draw(235:0.3) node[rectangle,fill=black,scale=0.5](d){};
\draw(275:0.8) node[rectangle,fill=black,scale=0.5](e){};
\draw(320:0.6) node[rectangle,fill=black,scale=0.5](f){};
\draw[-,dashed](-2,0)--++(a);
\draw[-,dashed](-2,0)--++(b);
\draw[-,dashed](-2,0)--++(c);
\draw[-,dashed](-2,0)--++(d);
\draw[-,dashed](-2,0)--++(e);
\draw[-,dashed](-2,0)--++(f);
\node[left] at(-2,0){Sensing Devices};
\end{tikzpicture}
\end{document}
答案1
您可以使用\foreach
循环:
\foreach \Point/\Angle\Dist in {a/55/0.5,b/120/0.8,c/210/0.7,d/235/0.3,e/275/0.8,f/320/0.6} {%
\draw(\Angle:\Dist) node[rectangle,fill=black,scale=0.5](\Point){};
\draw[-,dashed](-2,0)--++(\Point);
}
笔记:
- 在下面的 MWE 中,我删除了与问题无关的图片部分。
代码:
\documentclass{article}
\usepackage{pgfplots,tikz}
\usetikzlibrary{
positioning,
snakes,
shapes,
shadows
}
\begin{document}
\begin{tikzpicture}
\draw[fill=green!80] (0,0) circle (1);
\draw[fill=black](0,0)circle(0.5ex) node[right]{Access Point};
\foreach \Point/\Angle\Dist in {a/55/0.5,b/120/0.8,c/210/0.7,d/235/0.3,e/275/0.8,f/320/0.6} {%
\draw(\Angle:\Dist) node[rectangle,fill=black,scale=0.5](\Point){};
\draw[-,dashed](-2,0)--++(\Point);
}
\node[left] at(-2,0){Sensing Devices};
\end{tikzpicture}
\end{document}
答案2
你可以再减少一点代码。你可以删除节点的名称并直接绘制边
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[fill=green!80] (0,0) circle (1);
\draw[fill=black](0,0)circle(0.5ex) node[right]{Access Point};
\foreach \Angle\Dist in {55/0.5,120/0.8,210/0.7,235/0.3,275/0.8,320/0.6} {%
\draw[-,dashed](-2,0)--(\Angle:\Dist) coordinate[rectangle,fill=black,inner sep=0.17em];
}
\node[left] at(-2,0){Sensing Devices};
\end{tikzpicture}
\end{document}