試試使用 tikzpicture 繪製彩色 3x3x3 立方體

試試使用 tikzpicture 繪製彩色 3x3x3 立方體

我正在嘗試在 LateX 中繪製魔術方塊,並且我使用了下面的程式碼。除了前臉之外,一切似乎都有效,但由於某種原因,前臉不太對勁。有誰知道這個問題的解決方案?我正在使用 tikz 和 xcolor 包。

這是發生的事情

\begin{tikzpicture}
\coordinate (O) at (0,0,0);
\coordinate (A) at (1,0,0);
\coordinate (B) at (2,0,0);
\coordinate (C) at (3,0,0);
\coordinate (D) at (0,1,0);
\coordinate (G) at (1,1,0);
\coordinate (L) at (2,1,0);
\coordinate (N) at (3,1,0);
\coordinate (E) at (0,2,0);
\coordinate (J) at (1,2,0);
\coordinate (H) at (2,2,0);
\coordinate (P) at (3,2,0);
\coordinate (F) at (0,3,0);
\coordinate (K) at (1,3,0);
\coordinate (M) at (2,3,0);
\coordinate (I) at (3,3,0);
\coordinate (Q) at (3,0,1);
\coordinate (R) at (3,0,2);
\coordinate (S) at (3,0,3);
\coordinate (T) at (3,1,1);
\coordinate (U) at (3,1,2);
\coordinate (V) at (3,1,3);
\coordinate (W) at (3,2,1);
\coordinate (X) at (3,2,2);
\coordinate (Y) at (3,2,3);
\coordinate (Z) at (3,3,1);
\coordinate (AA) at (3,3,2);
\coordinate (BB) at (3,3,3);
\coordinate (CC) at (0,3,1);
\coordinate (DD) at (1,3,1);
\coordinate (EE) at (2,3,1);
\coordinate (FF) at (0,3,2);
\coordinate (GG) at (1,3,2);
\coordinate (HH) at (2,3,2);
\coordinate (II) at (0,3,3);
\coordinate (JJ) at (1,3,3);
\coordinate (KK) at (2,3,3);

\draw[black,fill=blue!80] (O) -- (A) -- (G) -- (D) -- cycle;
\draw[black,fill=blue!80] (A) -- (B) -- (L) -- (G) -- cycle;
\draw[black,fill=blue!80] (B) -- (C) -- (N) -- (L) -- cycle;
\draw[black,fill=blue!80] (D) -- (G) -- (J) -- (E) -- cycle;
\draw[black,fill=blue!80] (G) -- (L) -- (H) -- (J) -- cycle;
\draw[black,fill=blue!80] (L) -- (N) -- (P) -- (H) -- cycle;
\draw[black,fill=blue!80] (E) -- (J) -- (K) -- (F) -- cycle;
\draw[black,fill=blue!80] (J) -- (H) -- (M) -- (K) -- cycle;
\draw[black,fill=blue!80] (H) -- (P) -- (I) -- (M) -- cycle;

\draw[black,fill=white!80] (C) -- (Q) -- (T) -- (N) -- cycle;
\draw[black,fill=white!80] (Q) -- (R) -- (U) -- (T) -- cycle;
\draw[black,fill=white!80] (R) -- (S) -- (V) -- (U) -- cycle;
\draw[black,fill=white!80] (N) -- (T) -- (W) -- (P) -- cycle;
\draw[black,fill=white!80] (T) -- (U) -- (X) -- (W) -- cycle;
\draw[black,fill=white!80] (U) -- (V) -- (Y) -- (X) -- cycle;
\draw[black,fill=white!80] (P) -- (W) -- (Z) -- (I) -- cycle;
\draw[black,fill=white!80] (W) -- (X) -- (AA) -- (Z) -- cycle;
\draw[black,fill=white!80] (X) -- (Y) -- (BB) -- (AA) -- cycle;

\draw[black,fill=red!80] (F) -- (K) -- (DD) -- (CC) -- cycle;
\draw[black,fill=red!80] (K) -- (M) -- (EE) -- (DD) -- cycle;
\draw[black,fill=red!80] (M) -- (I) -- (Z) -- (EE) -- cycle;
\draw[black,fill=red!80] (CC) -- (DD) -- (GG) -- (FF) -- cycle;
\draw[black,fill=red!80] (DD) -- (EE) -- (HH) -- (GG) -- cycle;
\draw[black,fill=red!80] (EE) -- (Z) -- (AA) -- (HH) -- cycle;
\draw[black,fill=red!80] (FF) -- (GG) -- (JJ) -- (II) -- cycle;
\draw[black,fill=red!80] (GG) -- (HH) -- (KK) -- (JJ) -- cycle;
\draw[black,fill=red!80] (HH) -- (AA) -- (BB) -- (KK) -- cycle;

\end{tikzpicture}

答案1

你的問題是你畫的是背面,而不是正面。如果畫一個顯示單位向量的簡單圖表,您會發現:

\begin{tikzpicture}[->]
\draw (0,0,0) -- (1,0,0) node[right]{$x$};
\draw (0,0,0) -- (0,1,0) node[right]{$y$};
\draw (0,0,0) -- (0,0,1) node[right]{$z$};
\end{tikzpicture}

在此輸入影像描述

由於 z 軸指向頁面外,因此紅色和白色的面覆蓋了藍色面,並帶有您使用的座標。

因此解決這個問題的一個簡單方法是翻轉 z 單位向量。預設是這樣(-3.85mm,-3.85mm),所以你只需要做

\begin{tikzpicture}[z={(3.85mm,3.85mm)}]

你的圖表看起來像

在此輸入影像描述

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[z={(3.85mm,3.85mm)}]
\coordinate (O) at (0,0,0);
\coordinate (A) at (1,0,0);
\coordinate (B) at (2,0,0);
\coordinate (C) at (3,0,0);
\coordinate (D) at (0,1,0);
\coordinate (G) at (1,1,0);
\coordinate (L) at (2,1,0);
\coordinate (N) at (3,1,0);
\coordinate (E) at (0,2,0);
\coordinate (J) at (1,2,0);
\coordinate (H) at (2,2,0);
\coordinate (P) at (3,2,0);
\coordinate (F) at (0,3,0);
\coordinate (K) at (1,3,0);
\coordinate (M) at (2,3,0);
\coordinate (I) at (3,3,0);
\coordinate (Q) at (3,0,1);
\coordinate (R) at (3,0,2);
\coordinate (S) at (3,0,3);
\coordinate (T) at (3,1,1);
\coordinate (U) at (3,1,2);
\coordinate (V) at (3,1,3);
\coordinate (W) at (3,2,1);
\coordinate (X) at (3,2,2);
\coordinate (Y) at (3,2,3);
\coordinate (Z) at (3,3,1);
\coordinate (AA) at (3,3,2);
\coordinate (BB) at (3,3,3);
\coordinate (CC) at (0,3,1);
\coordinate (DD) at (1,3,1);
\coordinate (EE) at (2,3,1);
\coordinate (FF) at (0,3,2);
\coordinate (GG) at (1,3,2);
\coordinate (HH) at (2,3,2);
\coordinate (II) at (0,3,3);
\coordinate (JJ) at (1,3,3);
\coordinate (KK) at (2,3,3);

\draw[black,fill=blue!80] (O) -- (A) -- (G) -- (D) -- cycle;
\draw[black,fill=blue!80] (A) -- (B) -- (L) -- (G) -- cycle;
\draw[black,fill=blue!80] (B) -- (C) -- (N) -- (L) -- cycle;
\draw[black,fill=blue!80] (D) -- (G) -- (J) -- (E) -- cycle;
\draw[black,fill=blue!80] (G) -- (L) -- (H) -- (J) -- cycle;
\draw[black,fill=blue!80] (L) -- (N) -- (P) -- (H) -- cycle;
\draw[black,fill=blue!80] (E) -- (J) -- (K) -- (F) -- cycle;
\draw[black,fill=blue!80] (J) -- (H) -- (M) -- (K) -- cycle;
\draw[black,fill=blue!80] (H) -- (P) -- (I) -- (M) -- cycle;

\draw[black,fill=white!80] (C) -- (Q) -- (T) -- (N) -- cycle;
\draw[black,fill=white!80] (Q) -- (R) -- (U) -- (T) -- cycle;
\draw[black,fill=white!80] (R) -- (S) -- (V) -- (U) -- cycle;
\draw[black,fill=white!80] (N) -- (T) -- (W) -- (P) -- cycle;
\draw[black,fill=white!80] (T) -- (U) -- (X) -- (W) -- cycle;
\draw[black,fill=white!80] (U) -- (V) -- (Y) -- (X) -- cycle;
\draw[black,fill=white!80] (P) -- (W) -- (Z) -- (I) -- cycle;
\draw[black,fill=white!80] (W) -- (X) -- (AA) -- (Z) -- cycle;
\draw[black,fill=white!80] (X) -- (Y) -- (BB) -- (AA) -- cycle;

\draw[black,fill=red!80] (F) -- (K) -- (DD) -- (CC) -- cycle;
\draw[black,fill=red!80] (K) -- (M) -- (EE) -- (DD) -- cycle;
\draw[black,fill=red!80] (M) -- (I) -- (Z) -- (EE) -- cycle;
\draw[black,fill=red!80] (CC) -- (DD) -- (GG) -- (FF) -- cycle;
\draw[black,fill=red!80] (DD) -- (EE) -- (HH) -- (GG) -- cycle;
\draw[black,fill=red!80] (EE) -- (Z) -- (AA) -- (HH) -- cycle;
\draw[black,fill=red!80] (FF) -- (GG) -- (JJ) -- (II) -- cycle;
\draw[black,fill=red!80] (GG) -- (HH) -- (KK) -- (JJ) -- cycle;
\draw[black,fill=red!80] (HH) -- (AA) -- (BB) -- (KK) -- cycle;

\end{tikzpicture}
\end{document}

答案2

Torbjorn 所說的一切都是正確的,他的解決方案也是如此,但這段程式碼產生相同的結果,而且要短得多。

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[fill=blue!80] (0,0,3)--(3,0,3)--(3,3,3)--(0,3,3)--cycle;
\draw[fill=white!80] (3,0,0) -- (3,3,0)--(3,3,3)--(3,0,3)--cycle;
\draw[fill=red!80] (3,3,0) -- (3,3,3)--(0,3,3)--(0,3,0)--cycle;
\foreach \x in {1,2}
 {
\draw(3,0,\x)--(3,3,\x); 
\draw(3,\x,0)--(3,\x,3); 
\draw(0,\x,3)--(3,\x,3); 
\draw(\x,0,3)--(\x,3,3); 
\draw(\x,3,0)--(\x,3,3); 
\draw(0,3,\x)--(3,3,\x); 
}
\end{tikzpicture}
\end{document}

相關內容