![尝试使用 tikzpicture 绘制彩色 3x3x3 立方体](https://linux22.com/image/348404/%E5%B0%9D%E8%AF%95%E4%BD%BF%E7%94%A8%20tikzpicture%20%E7%BB%98%E5%88%B6%E5%BD%A9%E8%89%B2%203x3x3%20%E7%AB%8B%E6%96%B9%E4%BD%93.png)
我正在尝试在 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}