如何使用 TikZ 製作 3D 晶格?

如何使用 TikZ 製作 3D 晶格?

一段時間以來,我一直在嘗試使用 LaTeX 和 TikZ 製作一個像樣的 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}

相關內容