Versuch, mit TikzPicture einen farbigen 3x3x3-Würfel zu zeichnen

Versuch, mit TikzPicture einen farbigen 3x3x3-Würfel zu zeichnen

Ich versuche, einen Zauberwürfel in LateX zu zeichnen und habe den folgenden Code verwendet. Es scheint alles zu funktionieren, außer der Vorderseite, die aus irgendeinem Grund nicht richtig ist. Kennt jemand eine Lösung für dieses Problem? Ich verwende das Paket tikz und xcolor.

Folgendes passiert

\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}

Antwort1

Ihr Problem besteht darin, dass Sie die Rückseite und nicht die Vorderseite gezeichnet haben. Das können Sie erkennen, wenn Sie ein einfaches Diagramm mit den Einheitsvektoren zeichnen:

\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}

Bildbeschreibung hier eingeben

Da die Z-Achse aus der Seite herausragt, überdeckt die rot-weiße Fläche die blaue Fläche mit den von Ihnen verwendeten Koordinaten.

Eine einfache Möglichkeit, dies zu beheben, besteht darin, den Z-Einheitsvektor umzudrehen. Standardmäßig ist dies der Fall (-3.85mm,-3.85mm), Sie müssen also nur Folgendes tun:

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

und Ihr Diagramm sieht so aus

Bildbeschreibung hier eingeben

\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}

Antwort2

Alles, was Torbjörn sagt, ist richtig, ebenso wie seine Lösung, aber dieser Code führt zum selben Ergebnis und ist viel kürzer.

\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}

verwandte Informationen