My questions are:
- Is there a possibility to get a view of a Rubik's Cube in TikZ such as in
pgfplots
; likeview={NUMBER1}{NUMBER2}
? - How can be the Rubik's Cube be scrambled with a number
a
, such that this number makes a random pattern to the cube (→ scrambeling).
Here is my MWE (it's just the Cube without any functions):
\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{3d}
\begin{document}
\begin{tikzpicture}[every node/.style={inner sep=1cm,draw,very thick},very thick]
\draw[step=2cm,canvas is yz plane at x=0] (0,0) grid (8,8);
\node[fill=red] at (-4.08,-2.09) {};
\node[fill=white] at (-4.08,-.09) {};
\node[fill=blue] at (-4.08,1.91) {};
\node[fill=red] at (-4.08,3.91) {};
\begin{scope}[shift={(-2,0)}]
\node[fill=blue] at (-4.08,-2.09) {};
\node[fill=white] at (-4.08,-.09) {};
\node[fill=orange] at (-4.08,1.91) {};
\node[fill=white] at (-4.08,3.91) {};
\end{scope}
\begin{scope}[shift={(-4,0)}]
\node[fill=yellow] at (-4.08,-2.09) {};
\node[fill=blue] at (-4.08,-.09) {};
\node[fill=white] at (-4.08,1.91) {};
\node[fill=green] at (-4.08,3.91) {};
\begin{scope}[shift={(-2,0)}]
\node[fill=white] at (-4.08,-2.09) {};
\node[fill=red] at (-4.08,-.09) {};
\node[fill=blue] at (-4.08,1.91) {};
\node[fill=yellow] at (-4.08,3.91) {};
\end{scope}
\end{scope}
\draw[xshift=-8cm,yshift=8cm,step=2cm,canvas is xz plane at y=0] (0,0) grid (8,8);
\draw[fill=yellow,canvas is yz plane at x=0] (0,0) rectangle (2,2);
\draw[yshift=2cm,fill=blue,canvas is yz plane at x=0] (0,0) rectangle (2,2);
\draw[yshift=6cm,fill=orange,canvas is yz plane at x=0] (0,0) rectangle (2,2);
\begin{scope}[shift={(-.77,-.77)}]
\draw[fill=green,canvas is yz plane at x=0] (0,0) rectangle (2,2);
\draw[yshift=2cm,fill=orange,canvas is yz plane at x=0] (0,0) rectangle (2,2);
\draw[yshift=4cm,fill=orange,canvas is yz plane at x=0] (0,0) rectangle (2,2);
\draw[yshift=6cm,fill=blue,canvas is yz plane at x=0] (0,0) rectangle (2,2);
\end{scope}
\begin{scope}[shift={(2*-.77,2*-.77)}]
\draw[fill=red,canvas is yz plane at x=0] (0,0) rectangle (2,2);
\draw[yshift=2cm,fill=yellow,canvas is yz plane at x=0] (0,0) rectangle (2,2);
\draw[yshift=4cm,fill=red,canvas is yz plane at x=0] (0,0) rectangle (2,2);
\draw[yshift=6cm,fill=red,canvas is yz plane at x=0] (0,0) rectangle (2,2);
\end{scope}
\begin{scope}[shift={(3*-.77,3*-.77)}]
\draw[fill=blue,canvas is yz plane at x=0] (0,0) rectangle (2,2);
\draw[yshift=2cm,fill=orange,canvas is yz plane at x=0] (0,0) rectangle (2,2);
\draw[yshift=4cm,fill=yellow,canvas is yz plane at x=0] (0,0) rectangle (2,2);
\draw[yshift=6cm,fill=blue,canvas is yz plane at x=0] (0,0) rectangle (2,2);
\end{scope}
\draw[yshift=8cm,xshift=-2cm,fill=blue,canvas is xz plane at y=0] (0,0) rectangle (2,2);
\draw[yshift=7.23cm,xshift=-2.76cm,fill=orange,canvas is xz plane at y=0] (0,0) rectangle (2,2);
\draw[yshift=6.46cm,xshift=-3.55cm,fill=green,canvas is xz plane at y=0] (0,0) rectangle (2,2);
\begin{scope}[shift={(-2,0)}]
\draw[yshift=8cm,xshift=-2cm,fill=green,canvas is xz plane at y=0] (0,0) rectangle (2,2);
\draw[yshift=7.23cm,xshift=-2.76cm,fill=white,canvas is xz plane at y=0] (0,0) rectangle (2,2);
\draw[yshift=6.46cm,xshift=-3.55cm,fill=red,canvas is xz plane at y=0] (0,0) rectangle (2,2);
\draw[yshift=5.68cm,xshift=-4.33cm,fill=red,canvas is xz plane at y=0] (0,0) rectangle (2,2);
\end{scope}
\begin{scope}[shift={(-4,0)}]
\draw[yshift=8cm,xshift=-2cm,fill=yellow,canvas is xz plane at y=0] (0,0) rectangle (2,2);
\draw[yshift=7.23cm,xshift=-2.76cm,fill=yellow,canvas is xz plane at y=0] (0,0) rectangle (2,2);
\draw[yshift=6.46cm,xshift=-3.55cm,fill=green,canvas is xz plane at y=0] (0,0) rectangle (2,2);
\draw[yshift=5.68cm,xshift=-4.33cm,fill=red,canvas is xz plane at y=0] (0,0) rectangle (2,2);
\end{scope}
\begin{scope}[shift={(-6,0)}]
\draw[yshift=8cm,xshift=-2cm,fill=green,canvas is xz plane at y=0] (0,0) rectangle (2,2);
\draw[yshift=7.23cm,xshift=-2.76cm,fill=red,canvas is xz plane at y=0] (0,0) rectangle (2,2);
\draw[yshift=6.46cm,xshift=-3.55cm,fill=yellow,canvas is xz plane at y=0] (0,0) rectangle (2,2);
\draw[yshift=5.68cm,xshift=-4.33cm,fill=red,canvas is xz plane at y=0] (0,0) rectangle (2,2);
\end{scope}
\begin{scope}[yshift=-4cm]
\fill[black,canvas is zy plane at x=0] (0,0) rectangle (1.5,1.5);
\fill[xshift=-2.05cm,yshift=-.58cm] (0,0) rectangle (1.5,1.5);
\fill[xshift=-1.5cm,yshift=1.49cm,canvas is zx plane at y=0] (0,0) rectangle (1.5,1.5);
\fill[rounded corners=5,blue,canvas is zy plane at x=0] (0,0) rectangle (1.5,1.5);
\fill[rounded corners=5,xshift=-2.05cm,yshift=-.58cm,red] (0,0) rectangle (1.5,1.5);
\fill[rounded corners=5pt,xshift=-1.5cm,yshift=1.49cm,canvas is zx plane at y=0,white] (0,0) rectangle (1.5,1.5);
\node[fill=white,inner sep=0pt,draw=white] (n) at (-1.3,-1.7) {Another style};
\draw[line width=1pt,->] (n) --+ (0,1);
\end{scope}
\end{tikzpicture}
\end{document}
Here is the original picture:
And the output:
Best Answer
For your first question, a very simple example of how the
tikz-3dplot
handles its coordinate changes. Note the\tplotsetmaincoords{<angle>}{<angle>}
command that sets the view.I trust you'll be able to add the colors.
Edit
This is bit more realistic with rounded corners:
Edit 2
As per request, rotating one row:
The code becomes increasingly complex, and drawing order is very important.
To get it to rotate back and forth I cheated a bit when converting it to a
.gif
:Edit 3
This pretty much makes you able to control the rotation with buttons:
I added a command that draws a row of cubes, with optional
z
level (defaults to 2, zero based) and with a rotation aboutz
:\drawRotatedRow[<level>]{<rotation>}
. With this command now we can do something like this:Or even this (very long GIF):