到目前为止我所拥有的看起来相当平淡,因为我找不到从颜色列表中随机分配一种颜色的方法。
\documentclass[tikz]{standalone}
\def\colors{{red,green,blue,yellow}}
\begin{document}
\begin{tikzpicture}[]
\foreach \i in {1,...,12} {
\foreach \j in {1,...,6} {
\foreach \k in {1,...,4} {
\pgfmathparse{rnd}
\definecolor{randColor}{rgb}{\pgfmathresult,\pgfmathresult,\pgfmathresult}
\shade[ball color=randColor] (\i, {0.5*\j+\k}) circle(0.4);
}
}
}
\end{tikzpicture}
\end{document}
两个问题:
\colors{{red,green,blue,yellow}}
我怎样才能根据例如随机或看似随机地分配一个Mod(num, base)
到每个球?我无法得到数组索引上班:\shade[ball color=\colors[Mod(\i+\j+\k, 4)]
和
\pgfmathparse{\i+\j+\k} \shade[ball color=\colors[Mod(\pgfmathresult, 4)]]
都会引发错误。
是否可以旋转视角以类似于目标图像?
答案1
我认为最简单的方法是使用 在列表中定义颜色,\pgfmathdeclarerandomlist
然后使用 随机选择一个项目\pgfmathrandomitem
。由于您有 2DTikZ
图像,因此更改视图意味着更改球的位置(坐标)。
代码:
\documentclass[tikz]{standalone}
\pgfmathdeclarerandomlist{colors}{%
{red}%
{green}%
{blue}%
{yellow}%
}
\begin{document}
\begin{tikzpicture}[]
\foreach \i in {1,...,12} {
\foreach \j in {1,...,6} {
\foreach \k in {1,...,4} {
\pgfmathrandomitem{\randColor}{colors}
\shade[ball color=\randColor] (\i-\j/3, {0.5*\j+\k}) circle(0.4);
}
}
}
\end{tikzpicture}
\end{document}
顺便说一下,在 tikz 中绘制具有分子层的 3d 晶格向您展示构建这种“球”立方体的其他可能性。这些球也可以随机着色。例如,对于回答的杰利迪亚兹:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,fadings,decorations.pathreplacing}
\pgfmathdeclarerandomlist{colors}{%
{red}%
{green}%
{blue}%
{yellow}%
}
\begin{document}
% You can tweak these
\def\ballradius{0.45}
%
\def\DrawRow#1#2{
\foreach \x in {0,...,#2}
\pgfmathrandomitem{\randColor}{colors}
\shade[ball color=\randColor] ($(#1) +(\x, 0,0)$) circle(\ballradius);
}
\def\DrawOddPlane#1{
\pgfmathsetmacro{\aux}{#1-1}
\foreach \z in {0,...,#1} {
\DrawRow{0,0,\z}{#1}
\if\z#1\relax\else
\DrawRow{0.5,0,\z+0.5}{\aux}
\fi
}
}
\def\DrawEvenPlane#1{
\pgfmathsetmacro{\aux}{#1-1}
\foreach \z in {0,...,#1} {
\DrawRow{0.5,0,\z}{\aux}
\if\z#1\relax\else
\DrawRow{0,0,\z+0.5}{#1}
\fi
}
}
\begin{tikzpicture}
\foreach \y in {0,...,3} {
\begin{scope}[yshift=\y cm]
\DrawOddPlane{3}
\end{scope}
\if\y3\relax\else
\begin{scope}[yshift=\y cm + 0.5cm]
\DrawEvenPlane{3}
\end{scope}
\fi
}
\pgfmathsetmacro{\cubex}{1}
\pgfmathsetmacro{\cubey}{1}
\pgfmathsetmacro{\cubez}{1}
\draw (3,3,3) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
\draw (3,3,3) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
\draw (3,3,3) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{tikzpicture}
\end{document}
答案2
在@Ñako 的大力帮助下,这是最终结果。
% Cartoon of the AlCoCrFeNi high entropy alloy (HEA) with body-centered cubic (BCC) lattice.
\documentclass[tikz]{standalone}
\pgfmathdeclarerandomlist{colors}{{red!80}{teal}{blue!80}{orange}{blue!20}}
\begin{document}
\begin{tikzpicture}
\foreach \i in {1,...,12} {
\foreach \j in {1,...,4} {
\foreach \k in {1,...,4} {
\pgfmathrandomitem{\randColor}{colors}
\shade[ball color=\randColor] (-\i+0.3*\j, -0.2*\j+1.2*\k) circle(0.3);
}
\foreach \k in {1,...,3} {
\pgfmathrandomitem{\randColor}{colors}
\shade[ball color=\randColor] (-\i+0.5+0.3*\j, -0.2*\j+1.2*\k+0.6) circle(0.3);
}
}
}
\foreach \el/\color [count=\n] in {Al/red!80, Co/blue!80, Cr/teal, Fe/orange, Ni/blue!20} {
\shade[ball color=\color] (2, 5.5-\n) circle(0.3) node[right=1em] {\el};
}
\end{tikzpicture}
\end{document}