如何繪製立方體鏈?

如何繪製立方體鏈?

我正在嘗試找到在乳膠中繪製 3D 立方體鏈的最簡單方法。幫我在此輸入影像描述

我知道如何在 tikz 中繪製立方體。例如

\newcommand{\tikzcuboid}[4]{% width, height, depth, scale
\begin{tikzpicture}[scale=#4]
\foreach \x in {0,...,#1}
{   \draw (\x ,0  ,#3 ) -- (\x ,#2 ,#3 );

    \draw (\x ,#2 ,#3 ) -- (\x ,#2 ,0  );
    
   
}
\foreach \x in {0,...,#2}
{   \draw (#1 ,\x ,#3 ) -- (#1 ,\x ,0  );
    \draw (0  ,\x ,#3 ) -- (#1 ,\x ,#3 );
}
\foreach \x in {0,...,#3}
{   \draw (#1 ,0  ,\x ) -- (#1 ,#2 ,\x );
    \draw (0  ,#2 ,\x ) -- (#1 ,#2 ,\x );
}
\end{tikzpicture}
}

\newcommand{\tikzcube}[2]{% length, scale
\tikzcuboid{#1}{#1}{#1}{#2}
}

我在中找到這段程式碼需要協助從 TikZ 中的一組 2D 節點建立 3D 立方體

在此輸入影像描述

答案1

這是一個 tikz 解決方案。您可以更改\cubesAmount以繪製更多或更少的立方體。

在此輸入影像描述

\documentclass{article}
\usepackage{tikz}

\begin{document}
 
\begin{tikzpicture}[z={(0.5,0.5)}]

\def\cubesAmount{3}
\foreach \i in {1,...,\cubesAmount}{
    \draw (\i-1,\i-1,\i-1) rectangle +(1,1,0) -- ++(0,1,0) -- ++(0,0,1) -- ++(1,0,0) edge +(0,0,-1) -- ++(0,-1,0) -- ++(0,0,-1);
    \ifnum\i<\cubesAmount
        \node[anchor=north west] at (\i,\i,\i) {$(\i,\i,\i)$};
    \fi
}
\node[anchor=north east] at (0,0,0){$(0,0,0)$};
\node[anchor=south west] at (\cubesAmount,\cubesAmount,\cubesAmount){$(\cubesAmount,\cubesAmount,\cubesAmount)$};
 
\end{tikzpicture}
 
\end{document}

另外,如果你想改變視角,你可以調整z={(yaw,pitch)}

在此輸入影像描述

答案2

更新:一罐將漸近線程式碼包含在 LaTex 文件中。是這樣的

\documentclass{article}
\usepackage{asymptote}
\begin{document}
\begin{asy}
// can be directly run on http://asymptote.ualberta.ca/
<asymptote code>        
\end{asy}
\end{document}

我的建議是使用Asymptote,那麼一切都可用,內建:unitcube是一個曲面;unitbox是 3D 空間中的路徑/段的陣列。在這種情況下,我們可以自由選擇投影/視角(0,-2,1)

在此輸入影像描述

// http://asymptote.ualberta.ca/
import three;
size(5cm);
currentprojection=orthographic(0,-2,1,center=true,zoom=.8);
path3[] p=unitbox;
//surface p=unitcube;
draw(p,red);
draw(shift(1,1,1)*p,blue);
draw(shift(2,2,2)*p,magenta);

surface p=unitcube;

在此輸入影像描述

或者這個

在此輸入影像描述

// http://asymptote.ualberta.ca/
import three;
size(5cm);
currentprojection=orthographic(0,-2,1,center=true,zoom=.8);
path3[] p=unitbox;
surface q=unitcube;

draw(q,red+opacity(.1));
draw(shift(1,1,1)*q,blue+opacity(.1));
draw(shift(2,2,2)*q,magenta+opacity(.1));

draw(p,red);
draw(shift(1,1,1)*p,blue);
draw(shift(2,2,2)*p,magenta);

相關內容