我一直在尝试使用 LaTeX 和 TikZ 制作一个像样的 3D 晶格,最后我终于找到了一些代码,有人制作了一个完美的 3D 晶格。然而,无论我怎么尝试,我得到的不是 3D 晶格,而是一堆乱七八糟的随机取向的球形部分。
下面是我使用的代码(https://github.com/dog-blood/utterly-unorganized-latex-code/tree/main/Chem/Unit%203%20Gasses):
\begin{tikzpicture}[isometric view,rotate around z=180,line cap=round,line join=round,scale=0.8]
\tikzset{xy/.style={canvas is xy plane at z=\a,shift={(0.5*\a,0.5*\a)},rotate=90*\i,fill=red1,draw=pag!30},
xz/.style={canvas is xz plane at y=\a,shift={(0.5*\a,0.5*\a)},rotate=90*\i,fill=red1,draw=pag!30},
yz/.style={canvas is yz plane at x=\a,shift={(0.5*\a,0.5*\a)},rotate=90*\i,fill=red1,draw=pag!30},
my ball/.style={top color=red2, bottom color=red2}}
\def\a{3} % edge length
\pgfmathsetmacro\r{0.25*sqrt(2.5)*\a} % sphere radius
% background
\foreach\i in {0,120,240}
\draw[rotate=\i,color=pag!,my ball] (\a,0,0) --++ (0,\r,0) arc (90:135:\r) arc (0:60:\r cm)
{[canvas is xz plane at y=0] arc (135:90:\r)} -- cycle;
% sphere (center)
\draw[my ball, color=pag] (0.5*\a,0.5*\a,0.5*\a) circle (\r cm);
% foreground
\foreach\i in {0,120,240}
\draw[rotate=\i,my ball,color=pag] (\a,\a,0) --++ (60:\r cm) arc (60:120:\r cm) -- cycle;
\foreach\s in {xy,xz,yz} \foreach\i in {1,...,4}
\draw[\s] (45:{\a*sin(45)}) --++ (180:\r) arc (180:270:\r) -- cycle;
\end{tikzpicture}
答案1
我发现了我的错误,当时是清晨,我忘了添加:
\usetikzlibrary{perspective}