我正在尝试使用\foreach
循环来制作一些由线连接的交替圆圈链。我曾经\pgfmathparse
让小蓝色圆圈在大橙色圆圈的每一侧摆动。出于某种原因,pgfmathresult
在定位小蓝色圆圈时有效,但对于连接圆圈的线似乎不起作用。
\documentclass[tikz]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \i in {-3,...,3} {
\pgfmathparse{pow(-1,\i+3)*0.5};
\draw[fill=blue!30!white] (\i+0.5,\pgfmathresult) circle (0.1cm);
\draw(\i,0) -- (\i+0.5,\pgfmathresult) -- (\i+1,0);
\draw[fill=orange!60!white] (\i,0) circle (0.3cm);
}
\end{tikzpicture}
\end{document}
该输出应显示连接到相邻的小圆和大圆的实线。
答案1
\documentclass[tikz]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \i [evaluate=\i as \in using {pow(-1,\i+3)*0.5}] in {-3,...,3} {
% \pgfmathtruncatemacro{\in}{pow(-1,\i+3)*0.5};
\draw(\i,0) -- (\i+0.5,\in) -- (\i+1,0);
\draw[fill=blue!30!white] (\i+0.5,\in) circle (0.1cm);
% \draw(\i,0) -- (\i+0.5,\in) -- (\i+1,0);
\draw[fill=orange!60!white] (\i,0) circle (0.3cm);
}
\end{tikzpicture}
\end{document}
答案2
节点也一样:
\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\tikzset{
orangenode/.style={draw,circle,inner sep=0pt,minimum width=6mm,fill=orange!60},
bluenode/.style={draw,circle,inner sep=0pt,minimum width=2mm,fill=blue!30}
}
\begin{tikzpicture}
\foreach \i in {-3,...,3} {
\pgfmathsetmacro\j{pow(-1,\i+3)*0.5};
\node[orangenode] (O-\i) at (\i,0) {};
\node[bluenode] (B-\i) at (\i+0.5,\j) {};
}
\foreach \i in {-3,...,3} {
\draw (O-\i) -- (B-\i);
\pgfmathtruncatemacro\j{\i+1};
\ifnum \j<4 \draw (B-\i) -- (O-\j); \fi
}
\end{tikzpicture}
\end{document}