
\foreach
我正在編寫一些宏,允許在循環中以任意方式繪製球形狀:
\documentclass[tikz, border=0.5in]{standalone}
\newcommand{\ball}[3][0,0]{%\ball[basepoint]{point}{diameter}
\shadedraw[ball color=blue!15!white, draw=blue!50] (#1) +(#2) circle (#3);
}
\begin{document}
\begin{tikzpicture}
\foreach \t in {10, 20, ..., 360}{
\ball{\t:6}{0.5}
}
\ball{0,0}{0.5}
\foreach \t in {10, 20, ..., 360}{
\ball[14,0]{\t:6}{0.5}
}
\ball{14,0}{0.5}
\end{tikzpicture}
\end{document}
我使用相對座標是為了允許使用不以原點為中心的極座標,如上所示。不幸的是,我的陰影沒有按我的預期工作。
我希望得到這樣的結果,但使用相對座標:
\documentclass[tikz, border=0.5in]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \t in {10, 20, ..., 360}{
\shadedraw[ball color=blue!15!white, draw=blue!50] (\t:6) circle (0.5);
}
\shadedraw[ball color=blue!15!white, draw=blue!50] (0,0) circle (0.5)
\end{tikzpicture}
\end{document}
著色演算法似乎沒有考慮到繪製的圓的中心相對於 (0,0) 移動。是否可以重新定義陰影的來源?
答案1
為了限制著色路徑的邊界框,基點不應出現在該路徑中。
這裡有四種解決方案:
\ballshift
:shift
在您的中心使用操作,\ballscope
scope
:使用原點已轉移的本地,\ballcalc
:用來calc
轉移你的中心,\ballnode
:使用帶有陰影的圓形節點。
\documentclass[tikz, border=0.5in]{standalone}
\usetikzlibrary{calc}
\newcommand{\ballshift}[3][0,0]{%\ball[basepoint]{point}{diameter}
\shadedraw[ball color=blue!15!white, draw=blue!50] ([shift={(#1)}]#2) circle (#3);
}
\newcommand{\ballscope}[3][0,0]{%\ball[basepoint]{point}{diameter}
\begin{scope}[shift={(#1)}]
\shadedraw[ball color=red!15!white, draw=red!50] (#2) circle (#3);
\end{scope}
}
\newcommand{\ballcalc}[3][0,0]{%\ball[basepoint]{point}{diameter}
\shadedraw[ball color=orange!15!white, draw=orange!50] ($(#1)+(#2)$) circle (#3);
}
\newcommand{\ballnode}[3][0,0]{%\ball[basepoint]{point}{diameter}
\path (#1) ++(#2) node[inner sep=0,circle,minimum size=2cm*#3,
ball color=lime!50!white,draw=lime]{};
}
\begin{document}
\begin{tikzpicture}
\foreach \t in {10, 20, ..., 360}{
\ballshift{\t:6}{0.5}
\ballscope{\t:5}{0.4}
\ballcalc{\t:4}{0.3}
\ballnode{\t:3}{0.2}
}
\ballshift{0,0}{0.5}
\foreach \t in {10, 20, ..., 360}{
\ballshift[14,0]{\t:6}{0.5}
\ballscope[14,0]{\t:5}{0.4}
\ballcalc[14,0]{\t:4}{0.3}
\ballnode[14,0]{\t:3}{0.2}
}
\ballshift{14,0}{0.5}
\end{tikzpicture}
\end{document}