Playing around with a Rubik's Cube in TikZ
Clash Royale CLAN TAG#URR8PPP
up vote
21
down vote
favorite
My questions are:
- Is there a possibility to get a view of a Rubik's Cube in TikZ such as in
pgfplots
; likeview=NUMBER1NUMBER2
? - 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
usetikzlibrary3d
begindocument
begintikzpicture[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) ;
beginscope[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) ;
endscope
beginscope[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) ;
beginscope[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) ;
endscope
endscope
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);
beginscope[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);
endscope
beginscope[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);
endscope
beginscope[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);
endscope
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);
beginscope[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);
endscope
beginscope[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);
endscope
beginscope[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);
endscope
beginscope[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);
endscope
endtikzpicture
enddocument
Here is the original picture:
And the output:
tikz-pgf tikz-styles random
 |Â
show 3 more comments
up vote
21
down vote
favorite
My questions are:
- Is there a possibility to get a view of a Rubik's Cube in TikZ such as in
pgfplots
; likeview=NUMBER1NUMBER2
? - 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
usetikzlibrary3d
begindocument
begintikzpicture[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) ;
beginscope[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) ;
endscope
beginscope[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) ;
beginscope[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) ;
endscope
endscope
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);
beginscope[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);
endscope
beginscope[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);
endscope
beginscope[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);
endscope
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);
beginscope[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);
endscope
beginscope[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);
endscope
beginscope[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);
endscope
beginscope[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);
endscope
endtikzpicture
enddocument
Here is the original picture:
And the output:
tikz-pgf tikz-styles random
4
For your first question, I think you should look at thetikz-3dplot
package. Thetdplotsetmaincoords
command is what you need I think. Also have a look at this question.
â Max Snippe
Aug 6 at 10:51
@Max_Snippe: No, this isn't what I was looking for â I want to (1) to get a view which can be change by two parameters and (2) get TikZ to scramble the cube, but thank you anyway.
â current_user
Aug 6 at 10:53
2
Damn I really don't have the time to write elaborate answers but you keep testing my procrastination 'skills' with these cool questions.
â Max Snippe
Aug 6 at 11:10
2
Do you know the Rubik cube packages ctan.org/search/?phrase=rubik ? Maybe they could be used to simply the display etc.
â samcarter
Aug 6 at 11:11
1
Note that randomizing the colors is not easy if you want to guarantee a solvable cube. Only showing 3 sides makes it easier, but still not trivial.
â Matt
Aug 6 at 15:21
 |Â
show 3 more comments
up vote
21
down vote
favorite
up vote
21
down vote
favorite
My questions are:
- Is there a possibility to get a view of a Rubik's Cube in TikZ such as in
pgfplots
; likeview=NUMBER1NUMBER2
? - 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
usetikzlibrary3d
begindocument
begintikzpicture[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) ;
beginscope[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) ;
endscope
beginscope[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) ;
beginscope[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) ;
endscope
endscope
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);
beginscope[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);
endscope
beginscope[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);
endscope
beginscope[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);
endscope
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);
beginscope[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);
endscope
beginscope[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);
endscope
beginscope[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);
endscope
beginscope[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);
endscope
endtikzpicture
enddocument
Here is the original picture:
And the output:
tikz-pgf tikz-styles random
My questions are:
- Is there a possibility to get a view of a Rubik's Cube in TikZ such as in
pgfplots
; likeview=NUMBER1NUMBER2
? - 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
usetikzlibrary3d
begindocument
begintikzpicture[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) ;
beginscope[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) ;
endscope
beginscope[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) ;
beginscope[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) ;
endscope
endscope
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);
beginscope[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);
endscope
beginscope[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);
endscope
beginscope[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);
endscope
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);
beginscope[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);
endscope
beginscope[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);
endscope
beginscope[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);
endscope
beginscope[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);
endscope
endtikzpicture
enddocument
Here is the original picture:
And the output:
tikz-pgf tikz-styles random
edited Aug 6 at 12:10
asked Aug 6 at 10:49
current_user
1,200121
1,200121
4
For your first question, I think you should look at thetikz-3dplot
package. Thetdplotsetmaincoords
command is what you need I think. Also have a look at this question.
â Max Snippe
Aug 6 at 10:51
@Max_Snippe: No, this isn't what I was looking for â I want to (1) to get a view which can be change by two parameters and (2) get TikZ to scramble the cube, but thank you anyway.
â current_user
Aug 6 at 10:53
2
Damn I really don't have the time to write elaborate answers but you keep testing my procrastination 'skills' with these cool questions.
â Max Snippe
Aug 6 at 11:10
2
Do you know the Rubik cube packages ctan.org/search/?phrase=rubik ? Maybe they could be used to simply the display etc.
â samcarter
Aug 6 at 11:11
1
Note that randomizing the colors is not easy if you want to guarantee a solvable cube. Only showing 3 sides makes it easier, but still not trivial.
â Matt
Aug 6 at 15:21
 |Â
show 3 more comments
4
For your first question, I think you should look at thetikz-3dplot
package. Thetdplotsetmaincoords
command is what you need I think. Also have a look at this question.
â Max Snippe
Aug 6 at 10:51
@Max_Snippe: No, this isn't what I was looking for â I want to (1) to get a view which can be change by two parameters and (2) get TikZ to scramble the cube, but thank you anyway.
â current_user
Aug 6 at 10:53
2
Damn I really don't have the time to write elaborate answers but you keep testing my procrastination 'skills' with these cool questions.
â Max Snippe
Aug 6 at 11:10
2
Do you know the Rubik cube packages ctan.org/search/?phrase=rubik ? Maybe they could be used to simply the display etc.
â samcarter
Aug 6 at 11:11
1
Note that randomizing the colors is not easy if you want to guarantee a solvable cube. Only showing 3 sides makes it easier, but still not trivial.
â Matt
Aug 6 at 15:21
4
4
For your first question, I think you should look at the
tikz-3dplot
package. The tdplotsetmaincoords
command is what you need I think. Also have a look at this question.â Max Snippe
Aug 6 at 10:51
For your first question, I think you should look at the
tikz-3dplot
package. The tdplotsetmaincoords
command is what you need I think. Also have a look at this question.â Max Snippe
Aug 6 at 10:51
@Max_Snippe: No, this isn't what I was looking for â I want to (1) to get a view which can be change by two parameters and (2) get TikZ to scramble the cube, but thank you anyway.
â current_user
Aug 6 at 10:53
@Max_Snippe: No, this isn't what I was looking for â I want to (1) to get a view which can be change by two parameters and (2) get TikZ to scramble the cube, but thank you anyway.
â current_user
Aug 6 at 10:53
2
2
Damn I really don't have the time to write elaborate answers but you keep testing my procrastination 'skills' with these cool questions.
â Max Snippe
Aug 6 at 11:10
Damn I really don't have the time to write elaborate answers but you keep testing my procrastination 'skills' with these cool questions.
â Max Snippe
Aug 6 at 11:10
2
2
Do you know the Rubik cube packages ctan.org/search/?phrase=rubik ? Maybe they could be used to simply the display etc.
â samcarter
Aug 6 at 11:11
Do you know the Rubik cube packages ctan.org/search/?phrase=rubik ? Maybe they could be used to simply the display etc.
â samcarter
Aug 6 at 11:11
1
1
Note that randomizing the colors is not easy if you want to guarantee a solvable cube. Only showing 3 sides makes it easier, but still not trivial.
â Matt
Aug 6 at 15:21
Note that randomizing the colors is not easy if you want to guarantee a solvable cube. Only showing 3 sides makes it easier, but still not trivial.
â Matt
Aug 6 at 15:21
 |Â
show 3 more comments
2 Answers
2
active
oldest
votes
up vote
43
down vote
accepted
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.
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usetikzlibrary3d
begindocument
foreach myPsi in 90,100,...,170
tdplotsetmaincoords70myPsi
begintikzpicture
clip (-8,-6) rectangle (8,6);
beginscope[tdplot_main_coords]
draw[step=2cm,canvas is yz plane at x=4] (-4.01,-4.01) grid (4,4);
draw[step=2cm,canvas is xz plane at y=4] (-4.01,-4.01) grid (4,4);
draw[step=2cm,canvas is yx plane at z=4] (-4.01,-4.01) grid (4,4);
endscope
endtikzpicture
enddocument
Edit
This is bit more realistic with rounded corners:
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usetikzlibrary3d
begindocument
pgfmathsetmacroradius0.1
foreach frontcolor [remember=frontcolor as sidecolor (initially blue)] in red,white,orange,blue
foreach myPsi in 90,100,...,170
tdplotsetmaincoords70myPsi
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
beginscope[tdplot_main_coords]
filldraw [canvas is yz plane at x=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is xz plane at y=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yx plane at z=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
foreach X in -1.5,-0.5,0.5
foreach Y in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,Y),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,Y),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is yx plane at z=1.5,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
endscope
endtikzpicture
enddocument
Edit 2
As per request, rotating one row:
The code becomes increasingly complex, and drawing order is very important.
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usetikzlibrary3d
begindocument
pgfmathsetmacroradius0.1
foreach frontcolor [remember=frontcolor as sidecolor (initially blue)] in red,white,orange,blue
foreach myPsi in 90,100,...,170
tdplotsetmaincoords70100
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
beginscope[tdplot_main_coords]
filldraw [canvas is yz plane at x=1.5] (-1.5,-1.5) rectangle (1.5,0.5);
filldraw [canvas is xz plane at y=1.5] (-1.5,-1.5) rectangle (1.5,0.5);
filldraw [canvas is yx plane at z=0.5] (-1.5,-1.5) rectangle (1.5,1.5);
foreach X in -1.5,-0.5,0.5
foreach Y in -1.5,-0.5
draw [canvas is yz plane at x=1.5,shift=(X,Y),fill=blue] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,Y),fill=red] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
tdplotsetrotatedcoords00-myPsi+90
beginscope[tdplot_rotated_coords]
foreach X in -1.5,-0.5,0.5
filldraw [canvas is yz plane at x=1.5,shift=(X,0.5)] (0,0) rectangle (1,1);
filldraw [canvas is xz plane at y=1.5,shift=(X,0.5)] (0,0) rectangle (1,1);
draw [canvas is yz plane at x=1.5,shift=(X,0.5),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,0.5),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
foreach Y in -1.5,-0.5,0.5
filldraw [canvas is yx plane at z=1.5,shift=(X,Y)] (0,0) rectangle (1,1);
draw [canvas is yx plane at z=1.5,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
endscope
endscope
endtikzpicture
enddocument
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:
documentclassarticle
usepackageanimate
usepackagetikz
usepackagetikz-3dplot
usetikzlibrary3d
newwriteOutFile%
immediateopenoutOutFile=tl.txt%
immediatewriteOutFile::0x0,1
foreach i in 2,...,36
immediatewriteOutFile::i%
immediatecloseoutOutFile
pgfmathsetmacroradius0.1
tdplotsetmaincoords70100
newcommanddrawRotatedRow[2][2]
pgfmathsetmacromyHeight-1.5+int(#1)
pgfmathsetmacromyPsi#2
pgfmathsetmacromySecondPsi-80+Mod(myPsi+80,90)
pgfmathtruncatemacromySegmentMod((myPsi+80)/90,4)
ifcasemySegment% segment 0
deffrontcolorred
defsidecolorblue
or% segment 1
deffrontcolorblue
defsidecolororange
or% segment 2
deffrontcolororange
defsidecolorwhite
or% segment 3
deffrontcolorwhite
defsidecolorred
fi
beginscope[tdplot_main_coords]
tdplotsetrotatedcoords00mySecondPsi
beginscope[tdplot_rotated_coords]
filldraw [canvas is yx plane at z=myHeight+1] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yz plane at x=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
filldraw [canvas is xz plane at y=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
foreach X in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,myHeight),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,myHeight),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
ifnum#1=2relax
foreach Y in -1.5,-0.5,0.5
draw [canvas is yx plane at z=myHeight+1,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
fi
endscope
endscope
begindocument
beginanimateinline[controls,loop,timeline=tl.txt]10
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
drawRotatedRow[0]0
drawRotatedRow[1]0
endtikzpicture
newframe
multiframe36iPsi=0+10%
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
drawRotatedRowiPsi
endtikzpicture
endanimateinline
enddocument
I added a command that draws a row of cubes, with optional z
level (defaults to 2, zero based) and with a rotation about z
: drawRotatedRow[<level>]<rotation>
. With this command now we can do something like this:
documentclass[tikz]standalone
usepackageanimate
usepackagetikz
usepackagetikz-3dplot
usetikzlibrary3d
pgfmathsetmacroradius0.1
tdplotsetmaincoords70100
newcommanddrawRotatedRow[2]
pgfmathsetmacromyHeight-1.5+int(#1)
pgfmathsetmacromyPsi#2
pgfmathsetmacromySecondPsi-80+Mod(myPsi+80,90)
pgfmathtruncatemacromySegmentMod((myPsi+80)/90,4)
ifcasemySegment% segment 0
deffrontcolorred
defsidecolorblue
or% segment 1
deffrontcolorblue
defsidecolororange
or% segment 2
deffrontcolororange
defsidecolorwhite
or% segment 3
deffrontcolorwhite
defsidecolorred
fi
beginscope[tdplot_main_coords]
tdplotsetrotatedcoords00mySecondPsi
beginscope[tdplot_rotated_coords]
filldraw [canvas is yx plane at z=myHeight+1] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yz plane at x=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
filldraw [canvas is xz plane at y=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
foreach X in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,myHeight),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,myHeight),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
ifnum#1=2relax
foreach Y in -1.5,-0.5,0.5
draw [canvas is yx plane at z=myHeight+1,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
fi
endscope
endscope
begindocument
foreach iPsi in 0,10,...,359
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
drawRotatedRow0-iPsi
drawRotatedRow10
drawRotatedRow2iPsi
endtikzpicture
enddocument
Or even this (very long GIF):
documentclass[tikz]standalone
usepackageanimate
usepackagetikz
usepackagetikz-3dplot
usetikzlibrary3d
pgfmathsetmacroradius0.1
tdplotsetmaincoords70100
newcommanddrawRotatedRow[2]
pgfmathsetmacromyHeight-1.5+int(#1)
pgfmathsetmacromyPsi#2
pgfmathsetmacromySecondPsi-80+Mod(myPsi+80,90)
pgfmathtruncatemacromySegmentMod((myPsi+80)/90,4)
ifcasemySegment% segment 0
deffrontcolorred
defsidecolorblue
or% segment 1
deffrontcolorblue
defsidecolororange
or% segment 2
deffrontcolororange
defsidecolorwhite
or% segment 3
deffrontcolorwhite
defsidecolorred
fi
beginscope[tdplot_main_coords]
tdplotsetrotatedcoords00mySecondPsi
beginscope[tdplot_rotated_coords]
filldraw [canvas is yx plane at z=myHeight+1] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yz plane at x=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
filldraw [canvas is xz plane at y=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
foreach X in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,myHeight),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,myHeight),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
ifnum#1=2relax
foreach Y in -1.5,-0.5,0.5
draw [canvas is yx plane at z=myHeight+1,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
fi
endscope
endscope
begindocument
foreach level in 0,1,2
foreach iPsi in 0,10,...,359
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
ifcaselevel % Level 0 rotating
drawRotatedRow0iPsi
drawRotatedRow10
drawRotatedRow20
or % Level 1 rotating
drawRotatedRow00
drawRotatedRow1iPsi
drawRotatedRow20
or % Level 2 rotating
drawRotatedRow00
drawRotatedRow10
drawRotatedRow2iPsi
fi
endtikzpicture
enddocument
@Max_Snippe: Nice, that's it almost; how can I adjust the view for any angle ÃÂ, àand ò of rotation?
â current_user
Aug 6 at 11:12
1
@current_user you can either use thetikz-3dplot
nativetdplotsetrotatedcoords
command, or have a look at this question and its answers.
â Max Snippe
Aug 6 at 11:13
@Max_Snippe: WOW ⦠this looks amazing! Is there a way that, for example, one row can be rotated 90 degrees and back (threaded in the animation)?
â current_user
Aug 6 at 15:24
1
@current_user it's possible, but it gets quite hard quite fast. I don't think it is possible to automate the rotation of arbitrary rows and columns.
â Max Snippe
Aug 6 at 16:10
1
@current_user See my latest edit, I believe that it might be possible to expand this to rotating columns, but I will not try to do that. I leave some work for you :)
â Max Snippe
Aug 6 at 19:47
 |Â
show 1 more comment
up vote
19
down vote
Just for completeness. As there was the question about pgfplots, I just spell out Max Snippe's comment.
documentclass[border=3.14mm,tikz]standalone
usepackagepgfplots
pgfplotssetcompat=1.16
begindocument
defamax4 %number of squares in each direction
foreach X in 30
begintikzpicture
%path[use as bounding box] (-5,5) rectangle (5,5);
beginaxis[height=5cm,unit vector ratio=1 1 1,view=X20,colormap/hot,
set layers=standard,
domain=0:amax+1,
domain y=0:amax+1,
samples y=1,
xmin=-1,ymax=amax+1,
hide axis,
xtick=empty, ytick=empty, ztick=empty,
clip=false,samples=amax+1,samples y=amax+1
]
addplot3[point meta=rand,mesh,surf] (x,y,amax+1);
addplot3[point meta=rand,mesh,surf] (x,0,y);
addplot3[point meta=rand,mesh,surf] (amax+1,x,y);
endaxis
endtikzpicture
enddocument
Note:
I was not able to do a proper animation. For some reason the plots got doubled on each slide. I have no idea what's going on, and I was able to do proper pgfplots animations in the past. Most likely I am doing something really dumb.I was really dumb. For another question I changed the viewers preference to two page view. So one could do animations but compared to Max Snippe's result the outcome will be poor.If you give me rough idea what the colors should be I will be happy to add them as well. Ideally this would be some cool formula such that one could useI added random colors from a colormap that describes the temperatures these days. If I was not hibernating in winter, I could change it topoint meta
for that, otherwise I guess one has to resort to tables.cool
then.
ADDENDUM: All credits go to current_user, who had the idea, Max Snippe, who made the superb code (which I just stole), and samcarter, the author of tikzmarmots
. ;-)
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usepackagetikzmarmots
newsaveboxMarmot
saveboxMarmottikz[scale=0.4]marmot[whiskers,teeth]
usetikzlibrary3d
begindocument
pgfmathsetmacroradius0.1
foreach frontcolor [remember=frontcolor as sidecolor (initially blue)] in red,white,orange,blue
foreach myPsi in 90,100,...,170 %
tdplotsetmaincoords70myPsi
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
beginscope[tdplot_main_coords]
filldraw [canvas is yz plane at x=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is xz plane at y=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yx plane at z=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
foreach X in -1.5,-0.5,0.5
foreach Y in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,Y),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,Y),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is yx plane at z=1.5,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
beginscope[canvas is yz plane at x=1.5,transform shape]
node at (X+0.5,Y+0.5) useboxMarmot;
endscope
beginscope[canvas is xz plane at y=1.5,transform shape]
node at (X+0.5,Y+0.5) useboxMarmot;
endscope
beginscope[canvas is yx plane at z=1.5,transform shape]
node[yscale=-1] at (X+0.5,Y+0.5) useboxMarmot;
endscope
endscope
endtikzpicture
enddocument
Barbara Beeton discovered that the marmots are wagging their tails. This is because Max Snippe's routines are automatically such that the tails of the marmots are always behind the marmot. There is a reflection at the right moment. Therefore one may want to promote the marmots to 3D. (I am very optimistic that the tikzmarmots
package will provide 3D marmots in the near future. The impatient users may get the following animation by replacing all fill[
commands by shade[ball color=
in that package and modify the last marmot node to node[yscale=-1,rotate=myPsi-90] at (X+0.5,Y+0.5) useboxMarmot;
.)
1
This is great! Shame that you already had my +1
â Max Snippe
Aug 6 at 18:07
2
+1, for the turn of the tails from six to nine o'clock and the reset on the green side
â J Leon V.
Aug 6 at 23:32
1
@JLeonV. Thanks. That has already lead to some amusement in the chat ;-) But actually I didn't know about it before barbara beeton pointed it out, meaning that the projection by the 3d library does it on its own ... ;-) The reset is due to Max Snippe!
â marmot
Aug 6 at 23:35
2
@current_user Now I understand why that happens. Max Snippe's routine is more tricky than I thought. To understand this, you need to replace the marmots on the sides by different letters in the first two scopes.
â marmot
Aug 7 at 0:08
1
@marmot: As Max Snippet already mentioned: The drawing order is very important ⦠;)
â current_user
Aug 7 at 0:10
 |Â
show 7 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
43
down vote
accepted
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.
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usetikzlibrary3d
begindocument
foreach myPsi in 90,100,...,170
tdplotsetmaincoords70myPsi
begintikzpicture
clip (-8,-6) rectangle (8,6);
beginscope[tdplot_main_coords]
draw[step=2cm,canvas is yz plane at x=4] (-4.01,-4.01) grid (4,4);
draw[step=2cm,canvas is xz plane at y=4] (-4.01,-4.01) grid (4,4);
draw[step=2cm,canvas is yx plane at z=4] (-4.01,-4.01) grid (4,4);
endscope
endtikzpicture
enddocument
Edit
This is bit more realistic with rounded corners:
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usetikzlibrary3d
begindocument
pgfmathsetmacroradius0.1
foreach frontcolor [remember=frontcolor as sidecolor (initially blue)] in red,white,orange,blue
foreach myPsi in 90,100,...,170
tdplotsetmaincoords70myPsi
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
beginscope[tdplot_main_coords]
filldraw [canvas is yz plane at x=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is xz plane at y=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yx plane at z=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
foreach X in -1.5,-0.5,0.5
foreach Y in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,Y),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,Y),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is yx plane at z=1.5,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
endscope
endtikzpicture
enddocument
Edit 2
As per request, rotating one row:
The code becomes increasingly complex, and drawing order is very important.
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usetikzlibrary3d
begindocument
pgfmathsetmacroradius0.1
foreach frontcolor [remember=frontcolor as sidecolor (initially blue)] in red,white,orange,blue
foreach myPsi in 90,100,...,170
tdplotsetmaincoords70100
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
beginscope[tdplot_main_coords]
filldraw [canvas is yz plane at x=1.5] (-1.5,-1.5) rectangle (1.5,0.5);
filldraw [canvas is xz plane at y=1.5] (-1.5,-1.5) rectangle (1.5,0.5);
filldraw [canvas is yx plane at z=0.5] (-1.5,-1.5) rectangle (1.5,1.5);
foreach X in -1.5,-0.5,0.5
foreach Y in -1.5,-0.5
draw [canvas is yz plane at x=1.5,shift=(X,Y),fill=blue] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,Y),fill=red] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
tdplotsetrotatedcoords00-myPsi+90
beginscope[tdplot_rotated_coords]
foreach X in -1.5,-0.5,0.5
filldraw [canvas is yz plane at x=1.5,shift=(X,0.5)] (0,0) rectangle (1,1);
filldraw [canvas is xz plane at y=1.5,shift=(X,0.5)] (0,0) rectangle (1,1);
draw [canvas is yz plane at x=1.5,shift=(X,0.5),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,0.5),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
foreach Y in -1.5,-0.5,0.5
filldraw [canvas is yx plane at z=1.5,shift=(X,Y)] (0,0) rectangle (1,1);
draw [canvas is yx plane at z=1.5,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
endscope
endscope
endtikzpicture
enddocument
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:
documentclassarticle
usepackageanimate
usepackagetikz
usepackagetikz-3dplot
usetikzlibrary3d
newwriteOutFile%
immediateopenoutOutFile=tl.txt%
immediatewriteOutFile::0x0,1
foreach i in 2,...,36
immediatewriteOutFile::i%
immediatecloseoutOutFile
pgfmathsetmacroradius0.1
tdplotsetmaincoords70100
newcommanddrawRotatedRow[2][2]
pgfmathsetmacromyHeight-1.5+int(#1)
pgfmathsetmacromyPsi#2
pgfmathsetmacromySecondPsi-80+Mod(myPsi+80,90)
pgfmathtruncatemacromySegmentMod((myPsi+80)/90,4)
ifcasemySegment% segment 0
deffrontcolorred
defsidecolorblue
or% segment 1
deffrontcolorblue
defsidecolororange
or% segment 2
deffrontcolororange
defsidecolorwhite
or% segment 3
deffrontcolorwhite
defsidecolorred
fi
beginscope[tdplot_main_coords]
tdplotsetrotatedcoords00mySecondPsi
beginscope[tdplot_rotated_coords]
filldraw [canvas is yx plane at z=myHeight+1] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yz plane at x=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
filldraw [canvas is xz plane at y=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
foreach X in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,myHeight),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,myHeight),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
ifnum#1=2relax
foreach Y in -1.5,-0.5,0.5
draw [canvas is yx plane at z=myHeight+1,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
fi
endscope
endscope
begindocument
beginanimateinline[controls,loop,timeline=tl.txt]10
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
drawRotatedRow[0]0
drawRotatedRow[1]0
endtikzpicture
newframe
multiframe36iPsi=0+10%
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
drawRotatedRowiPsi
endtikzpicture
endanimateinline
enddocument
I added a command that draws a row of cubes, with optional z
level (defaults to 2, zero based) and with a rotation about z
: drawRotatedRow[<level>]<rotation>
. With this command now we can do something like this:
documentclass[tikz]standalone
usepackageanimate
usepackagetikz
usepackagetikz-3dplot
usetikzlibrary3d
pgfmathsetmacroradius0.1
tdplotsetmaincoords70100
newcommanddrawRotatedRow[2]
pgfmathsetmacromyHeight-1.5+int(#1)
pgfmathsetmacromyPsi#2
pgfmathsetmacromySecondPsi-80+Mod(myPsi+80,90)
pgfmathtruncatemacromySegmentMod((myPsi+80)/90,4)
ifcasemySegment% segment 0
deffrontcolorred
defsidecolorblue
or% segment 1
deffrontcolorblue
defsidecolororange
or% segment 2
deffrontcolororange
defsidecolorwhite
or% segment 3
deffrontcolorwhite
defsidecolorred
fi
beginscope[tdplot_main_coords]
tdplotsetrotatedcoords00mySecondPsi
beginscope[tdplot_rotated_coords]
filldraw [canvas is yx plane at z=myHeight+1] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yz plane at x=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
filldraw [canvas is xz plane at y=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
foreach X in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,myHeight),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,myHeight),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
ifnum#1=2relax
foreach Y in -1.5,-0.5,0.5
draw [canvas is yx plane at z=myHeight+1,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
fi
endscope
endscope
begindocument
foreach iPsi in 0,10,...,359
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
drawRotatedRow0-iPsi
drawRotatedRow10
drawRotatedRow2iPsi
endtikzpicture
enddocument
Or even this (very long GIF):
documentclass[tikz]standalone
usepackageanimate
usepackagetikz
usepackagetikz-3dplot
usetikzlibrary3d
pgfmathsetmacroradius0.1
tdplotsetmaincoords70100
newcommanddrawRotatedRow[2]
pgfmathsetmacromyHeight-1.5+int(#1)
pgfmathsetmacromyPsi#2
pgfmathsetmacromySecondPsi-80+Mod(myPsi+80,90)
pgfmathtruncatemacromySegmentMod((myPsi+80)/90,4)
ifcasemySegment% segment 0
deffrontcolorred
defsidecolorblue
or% segment 1
deffrontcolorblue
defsidecolororange
or% segment 2
deffrontcolororange
defsidecolorwhite
or% segment 3
deffrontcolorwhite
defsidecolorred
fi
beginscope[tdplot_main_coords]
tdplotsetrotatedcoords00mySecondPsi
beginscope[tdplot_rotated_coords]
filldraw [canvas is yx plane at z=myHeight+1] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yz plane at x=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
filldraw [canvas is xz plane at y=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
foreach X in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,myHeight),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,myHeight),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
ifnum#1=2relax
foreach Y in -1.5,-0.5,0.5
draw [canvas is yx plane at z=myHeight+1,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
fi
endscope
endscope
begindocument
foreach level in 0,1,2
foreach iPsi in 0,10,...,359
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
ifcaselevel % Level 0 rotating
drawRotatedRow0iPsi
drawRotatedRow10
drawRotatedRow20
or % Level 1 rotating
drawRotatedRow00
drawRotatedRow1iPsi
drawRotatedRow20
or % Level 2 rotating
drawRotatedRow00
drawRotatedRow10
drawRotatedRow2iPsi
fi
endtikzpicture
enddocument
@Max_Snippe: Nice, that's it almost; how can I adjust the view for any angle ÃÂ, àand ò of rotation?
â current_user
Aug 6 at 11:12
1
@current_user you can either use thetikz-3dplot
nativetdplotsetrotatedcoords
command, or have a look at this question and its answers.
â Max Snippe
Aug 6 at 11:13
@Max_Snippe: WOW ⦠this looks amazing! Is there a way that, for example, one row can be rotated 90 degrees and back (threaded in the animation)?
â current_user
Aug 6 at 15:24
1
@current_user it's possible, but it gets quite hard quite fast. I don't think it is possible to automate the rotation of arbitrary rows and columns.
â Max Snippe
Aug 6 at 16:10
1
@current_user See my latest edit, I believe that it might be possible to expand this to rotating columns, but I will not try to do that. I leave some work for you :)
â Max Snippe
Aug 6 at 19:47
 |Â
show 1 more comment
up vote
43
down vote
accepted
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.
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usetikzlibrary3d
begindocument
foreach myPsi in 90,100,...,170
tdplotsetmaincoords70myPsi
begintikzpicture
clip (-8,-6) rectangle (8,6);
beginscope[tdplot_main_coords]
draw[step=2cm,canvas is yz plane at x=4] (-4.01,-4.01) grid (4,4);
draw[step=2cm,canvas is xz plane at y=4] (-4.01,-4.01) grid (4,4);
draw[step=2cm,canvas is yx plane at z=4] (-4.01,-4.01) grid (4,4);
endscope
endtikzpicture
enddocument
Edit
This is bit more realistic with rounded corners:
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usetikzlibrary3d
begindocument
pgfmathsetmacroradius0.1
foreach frontcolor [remember=frontcolor as sidecolor (initially blue)] in red,white,orange,blue
foreach myPsi in 90,100,...,170
tdplotsetmaincoords70myPsi
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
beginscope[tdplot_main_coords]
filldraw [canvas is yz plane at x=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is xz plane at y=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yx plane at z=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
foreach X in -1.5,-0.5,0.5
foreach Y in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,Y),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,Y),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is yx plane at z=1.5,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
endscope
endtikzpicture
enddocument
Edit 2
As per request, rotating one row:
The code becomes increasingly complex, and drawing order is very important.
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usetikzlibrary3d
begindocument
pgfmathsetmacroradius0.1
foreach frontcolor [remember=frontcolor as sidecolor (initially blue)] in red,white,orange,blue
foreach myPsi in 90,100,...,170
tdplotsetmaincoords70100
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
beginscope[tdplot_main_coords]
filldraw [canvas is yz plane at x=1.5] (-1.5,-1.5) rectangle (1.5,0.5);
filldraw [canvas is xz plane at y=1.5] (-1.5,-1.5) rectangle (1.5,0.5);
filldraw [canvas is yx plane at z=0.5] (-1.5,-1.5) rectangle (1.5,1.5);
foreach X in -1.5,-0.5,0.5
foreach Y in -1.5,-0.5
draw [canvas is yz plane at x=1.5,shift=(X,Y),fill=blue] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,Y),fill=red] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
tdplotsetrotatedcoords00-myPsi+90
beginscope[tdplot_rotated_coords]
foreach X in -1.5,-0.5,0.5
filldraw [canvas is yz plane at x=1.5,shift=(X,0.5)] (0,0) rectangle (1,1);
filldraw [canvas is xz plane at y=1.5,shift=(X,0.5)] (0,0) rectangle (1,1);
draw [canvas is yz plane at x=1.5,shift=(X,0.5),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,0.5),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
foreach Y in -1.5,-0.5,0.5
filldraw [canvas is yx plane at z=1.5,shift=(X,Y)] (0,0) rectangle (1,1);
draw [canvas is yx plane at z=1.5,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
endscope
endscope
endtikzpicture
enddocument
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:
documentclassarticle
usepackageanimate
usepackagetikz
usepackagetikz-3dplot
usetikzlibrary3d
newwriteOutFile%
immediateopenoutOutFile=tl.txt%
immediatewriteOutFile::0x0,1
foreach i in 2,...,36
immediatewriteOutFile::i%
immediatecloseoutOutFile
pgfmathsetmacroradius0.1
tdplotsetmaincoords70100
newcommanddrawRotatedRow[2][2]
pgfmathsetmacromyHeight-1.5+int(#1)
pgfmathsetmacromyPsi#2
pgfmathsetmacromySecondPsi-80+Mod(myPsi+80,90)
pgfmathtruncatemacromySegmentMod((myPsi+80)/90,4)
ifcasemySegment% segment 0
deffrontcolorred
defsidecolorblue
or% segment 1
deffrontcolorblue
defsidecolororange
or% segment 2
deffrontcolororange
defsidecolorwhite
or% segment 3
deffrontcolorwhite
defsidecolorred
fi
beginscope[tdplot_main_coords]
tdplotsetrotatedcoords00mySecondPsi
beginscope[tdplot_rotated_coords]
filldraw [canvas is yx plane at z=myHeight+1] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yz plane at x=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
filldraw [canvas is xz plane at y=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
foreach X in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,myHeight),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,myHeight),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
ifnum#1=2relax
foreach Y in -1.5,-0.5,0.5
draw [canvas is yx plane at z=myHeight+1,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
fi
endscope
endscope
begindocument
beginanimateinline[controls,loop,timeline=tl.txt]10
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
drawRotatedRow[0]0
drawRotatedRow[1]0
endtikzpicture
newframe
multiframe36iPsi=0+10%
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
drawRotatedRowiPsi
endtikzpicture
endanimateinline
enddocument
I added a command that draws a row of cubes, with optional z
level (defaults to 2, zero based) and with a rotation about z
: drawRotatedRow[<level>]<rotation>
. With this command now we can do something like this:
documentclass[tikz]standalone
usepackageanimate
usepackagetikz
usepackagetikz-3dplot
usetikzlibrary3d
pgfmathsetmacroradius0.1
tdplotsetmaincoords70100
newcommanddrawRotatedRow[2]
pgfmathsetmacromyHeight-1.5+int(#1)
pgfmathsetmacromyPsi#2
pgfmathsetmacromySecondPsi-80+Mod(myPsi+80,90)
pgfmathtruncatemacromySegmentMod((myPsi+80)/90,4)
ifcasemySegment% segment 0
deffrontcolorred
defsidecolorblue
or% segment 1
deffrontcolorblue
defsidecolororange
or% segment 2
deffrontcolororange
defsidecolorwhite
or% segment 3
deffrontcolorwhite
defsidecolorred
fi
beginscope[tdplot_main_coords]
tdplotsetrotatedcoords00mySecondPsi
beginscope[tdplot_rotated_coords]
filldraw [canvas is yx plane at z=myHeight+1] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yz plane at x=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
filldraw [canvas is xz plane at y=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
foreach X in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,myHeight),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,myHeight),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
ifnum#1=2relax
foreach Y in -1.5,-0.5,0.5
draw [canvas is yx plane at z=myHeight+1,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
fi
endscope
endscope
begindocument
foreach iPsi in 0,10,...,359
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
drawRotatedRow0-iPsi
drawRotatedRow10
drawRotatedRow2iPsi
endtikzpicture
enddocument
Or even this (very long GIF):
documentclass[tikz]standalone
usepackageanimate
usepackagetikz
usepackagetikz-3dplot
usetikzlibrary3d
pgfmathsetmacroradius0.1
tdplotsetmaincoords70100
newcommanddrawRotatedRow[2]
pgfmathsetmacromyHeight-1.5+int(#1)
pgfmathsetmacromyPsi#2
pgfmathsetmacromySecondPsi-80+Mod(myPsi+80,90)
pgfmathtruncatemacromySegmentMod((myPsi+80)/90,4)
ifcasemySegment% segment 0
deffrontcolorred
defsidecolorblue
or% segment 1
deffrontcolorblue
defsidecolororange
or% segment 2
deffrontcolororange
defsidecolorwhite
or% segment 3
deffrontcolorwhite
defsidecolorred
fi
beginscope[tdplot_main_coords]
tdplotsetrotatedcoords00mySecondPsi
beginscope[tdplot_rotated_coords]
filldraw [canvas is yx plane at z=myHeight+1] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yz plane at x=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
filldraw [canvas is xz plane at y=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
foreach X in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,myHeight),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,myHeight),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
ifnum#1=2relax
foreach Y in -1.5,-0.5,0.5
draw [canvas is yx plane at z=myHeight+1,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
fi
endscope
endscope
begindocument
foreach level in 0,1,2
foreach iPsi in 0,10,...,359
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
ifcaselevel % Level 0 rotating
drawRotatedRow0iPsi
drawRotatedRow10
drawRotatedRow20
or % Level 1 rotating
drawRotatedRow00
drawRotatedRow1iPsi
drawRotatedRow20
or % Level 2 rotating
drawRotatedRow00
drawRotatedRow10
drawRotatedRow2iPsi
fi
endtikzpicture
enddocument
@Max_Snippe: Nice, that's it almost; how can I adjust the view for any angle ÃÂ, àand ò of rotation?
â current_user
Aug 6 at 11:12
1
@current_user you can either use thetikz-3dplot
nativetdplotsetrotatedcoords
command, or have a look at this question and its answers.
â Max Snippe
Aug 6 at 11:13
@Max_Snippe: WOW ⦠this looks amazing! Is there a way that, for example, one row can be rotated 90 degrees and back (threaded in the animation)?
â current_user
Aug 6 at 15:24
1
@current_user it's possible, but it gets quite hard quite fast. I don't think it is possible to automate the rotation of arbitrary rows and columns.
â Max Snippe
Aug 6 at 16:10
1
@current_user See my latest edit, I believe that it might be possible to expand this to rotating columns, but I will not try to do that. I leave some work for you :)
â Max Snippe
Aug 6 at 19:47
 |Â
show 1 more comment
up vote
43
down vote
accepted
up vote
43
down vote
accepted
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.
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usetikzlibrary3d
begindocument
foreach myPsi in 90,100,...,170
tdplotsetmaincoords70myPsi
begintikzpicture
clip (-8,-6) rectangle (8,6);
beginscope[tdplot_main_coords]
draw[step=2cm,canvas is yz plane at x=4] (-4.01,-4.01) grid (4,4);
draw[step=2cm,canvas is xz plane at y=4] (-4.01,-4.01) grid (4,4);
draw[step=2cm,canvas is yx plane at z=4] (-4.01,-4.01) grid (4,4);
endscope
endtikzpicture
enddocument
Edit
This is bit more realistic with rounded corners:
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usetikzlibrary3d
begindocument
pgfmathsetmacroradius0.1
foreach frontcolor [remember=frontcolor as sidecolor (initially blue)] in red,white,orange,blue
foreach myPsi in 90,100,...,170
tdplotsetmaincoords70myPsi
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
beginscope[tdplot_main_coords]
filldraw [canvas is yz plane at x=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is xz plane at y=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yx plane at z=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
foreach X in -1.5,-0.5,0.5
foreach Y in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,Y),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,Y),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is yx plane at z=1.5,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
endscope
endtikzpicture
enddocument
Edit 2
As per request, rotating one row:
The code becomes increasingly complex, and drawing order is very important.
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usetikzlibrary3d
begindocument
pgfmathsetmacroradius0.1
foreach frontcolor [remember=frontcolor as sidecolor (initially blue)] in red,white,orange,blue
foreach myPsi in 90,100,...,170
tdplotsetmaincoords70100
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
beginscope[tdplot_main_coords]
filldraw [canvas is yz plane at x=1.5] (-1.5,-1.5) rectangle (1.5,0.5);
filldraw [canvas is xz plane at y=1.5] (-1.5,-1.5) rectangle (1.5,0.5);
filldraw [canvas is yx plane at z=0.5] (-1.5,-1.5) rectangle (1.5,1.5);
foreach X in -1.5,-0.5,0.5
foreach Y in -1.5,-0.5
draw [canvas is yz plane at x=1.5,shift=(X,Y),fill=blue] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,Y),fill=red] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
tdplotsetrotatedcoords00-myPsi+90
beginscope[tdplot_rotated_coords]
foreach X in -1.5,-0.5,0.5
filldraw [canvas is yz plane at x=1.5,shift=(X,0.5)] (0,0) rectangle (1,1);
filldraw [canvas is xz plane at y=1.5,shift=(X,0.5)] (0,0) rectangle (1,1);
draw [canvas is yz plane at x=1.5,shift=(X,0.5),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,0.5),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
foreach Y in -1.5,-0.5,0.5
filldraw [canvas is yx plane at z=1.5,shift=(X,Y)] (0,0) rectangle (1,1);
draw [canvas is yx plane at z=1.5,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
endscope
endscope
endtikzpicture
enddocument
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:
documentclassarticle
usepackageanimate
usepackagetikz
usepackagetikz-3dplot
usetikzlibrary3d
newwriteOutFile%
immediateopenoutOutFile=tl.txt%
immediatewriteOutFile::0x0,1
foreach i in 2,...,36
immediatewriteOutFile::i%
immediatecloseoutOutFile
pgfmathsetmacroradius0.1
tdplotsetmaincoords70100
newcommanddrawRotatedRow[2][2]
pgfmathsetmacromyHeight-1.5+int(#1)
pgfmathsetmacromyPsi#2
pgfmathsetmacromySecondPsi-80+Mod(myPsi+80,90)
pgfmathtruncatemacromySegmentMod((myPsi+80)/90,4)
ifcasemySegment% segment 0
deffrontcolorred
defsidecolorblue
or% segment 1
deffrontcolorblue
defsidecolororange
or% segment 2
deffrontcolororange
defsidecolorwhite
or% segment 3
deffrontcolorwhite
defsidecolorred
fi
beginscope[tdplot_main_coords]
tdplotsetrotatedcoords00mySecondPsi
beginscope[tdplot_rotated_coords]
filldraw [canvas is yx plane at z=myHeight+1] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yz plane at x=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
filldraw [canvas is xz plane at y=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
foreach X in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,myHeight),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,myHeight),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
ifnum#1=2relax
foreach Y in -1.5,-0.5,0.5
draw [canvas is yx plane at z=myHeight+1,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
fi
endscope
endscope
begindocument
beginanimateinline[controls,loop,timeline=tl.txt]10
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
drawRotatedRow[0]0
drawRotatedRow[1]0
endtikzpicture
newframe
multiframe36iPsi=0+10%
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
drawRotatedRowiPsi
endtikzpicture
endanimateinline
enddocument
I added a command that draws a row of cubes, with optional z
level (defaults to 2, zero based) and with a rotation about z
: drawRotatedRow[<level>]<rotation>
. With this command now we can do something like this:
documentclass[tikz]standalone
usepackageanimate
usepackagetikz
usepackagetikz-3dplot
usetikzlibrary3d
pgfmathsetmacroradius0.1
tdplotsetmaincoords70100
newcommanddrawRotatedRow[2]
pgfmathsetmacromyHeight-1.5+int(#1)
pgfmathsetmacromyPsi#2
pgfmathsetmacromySecondPsi-80+Mod(myPsi+80,90)
pgfmathtruncatemacromySegmentMod((myPsi+80)/90,4)
ifcasemySegment% segment 0
deffrontcolorred
defsidecolorblue
or% segment 1
deffrontcolorblue
defsidecolororange
or% segment 2
deffrontcolororange
defsidecolorwhite
or% segment 3
deffrontcolorwhite
defsidecolorred
fi
beginscope[tdplot_main_coords]
tdplotsetrotatedcoords00mySecondPsi
beginscope[tdplot_rotated_coords]
filldraw [canvas is yx plane at z=myHeight+1] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yz plane at x=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
filldraw [canvas is xz plane at y=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
foreach X in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,myHeight),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,myHeight),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
ifnum#1=2relax
foreach Y in -1.5,-0.5,0.5
draw [canvas is yx plane at z=myHeight+1,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
fi
endscope
endscope
begindocument
foreach iPsi in 0,10,...,359
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
drawRotatedRow0-iPsi
drawRotatedRow10
drawRotatedRow2iPsi
endtikzpicture
enddocument
Or even this (very long GIF):
documentclass[tikz]standalone
usepackageanimate
usepackagetikz
usepackagetikz-3dplot
usetikzlibrary3d
pgfmathsetmacroradius0.1
tdplotsetmaincoords70100
newcommanddrawRotatedRow[2]
pgfmathsetmacromyHeight-1.5+int(#1)
pgfmathsetmacromyPsi#2
pgfmathsetmacromySecondPsi-80+Mod(myPsi+80,90)
pgfmathtruncatemacromySegmentMod((myPsi+80)/90,4)
ifcasemySegment% segment 0
deffrontcolorred
defsidecolorblue
or% segment 1
deffrontcolorblue
defsidecolororange
or% segment 2
deffrontcolororange
defsidecolorwhite
or% segment 3
deffrontcolorwhite
defsidecolorred
fi
beginscope[tdplot_main_coords]
tdplotsetrotatedcoords00mySecondPsi
beginscope[tdplot_rotated_coords]
filldraw [canvas is yx plane at z=myHeight+1] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yz plane at x=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
filldraw [canvas is xz plane at y=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
foreach X in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,myHeight),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,myHeight),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
ifnum#1=2relax
foreach Y in -1.5,-0.5,0.5
draw [canvas is yx plane at z=myHeight+1,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
fi
endscope
endscope
begindocument
foreach level in 0,1,2
foreach iPsi in 0,10,...,359
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
ifcaselevel % Level 0 rotating
drawRotatedRow0iPsi
drawRotatedRow10
drawRotatedRow20
or % Level 1 rotating
drawRotatedRow00
drawRotatedRow1iPsi
drawRotatedRow20
or % Level 2 rotating
drawRotatedRow00
drawRotatedRow10
drawRotatedRow2iPsi
fi
endtikzpicture
enddocument
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.
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usetikzlibrary3d
begindocument
foreach myPsi in 90,100,...,170
tdplotsetmaincoords70myPsi
begintikzpicture
clip (-8,-6) rectangle (8,6);
beginscope[tdplot_main_coords]
draw[step=2cm,canvas is yz plane at x=4] (-4.01,-4.01) grid (4,4);
draw[step=2cm,canvas is xz plane at y=4] (-4.01,-4.01) grid (4,4);
draw[step=2cm,canvas is yx plane at z=4] (-4.01,-4.01) grid (4,4);
endscope
endtikzpicture
enddocument
Edit
This is bit more realistic with rounded corners:
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usetikzlibrary3d
begindocument
pgfmathsetmacroradius0.1
foreach frontcolor [remember=frontcolor as sidecolor (initially blue)] in red,white,orange,blue
foreach myPsi in 90,100,...,170
tdplotsetmaincoords70myPsi
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
beginscope[tdplot_main_coords]
filldraw [canvas is yz plane at x=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is xz plane at y=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yx plane at z=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
foreach X in -1.5,-0.5,0.5
foreach Y in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,Y),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,Y),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is yx plane at z=1.5,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
endscope
endtikzpicture
enddocument
Edit 2
As per request, rotating one row:
The code becomes increasingly complex, and drawing order is very important.
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usetikzlibrary3d
begindocument
pgfmathsetmacroradius0.1
foreach frontcolor [remember=frontcolor as sidecolor (initially blue)] in red,white,orange,blue
foreach myPsi in 90,100,...,170
tdplotsetmaincoords70100
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
beginscope[tdplot_main_coords]
filldraw [canvas is yz plane at x=1.5] (-1.5,-1.5) rectangle (1.5,0.5);
filldraw [canvas is xz plane at y=1.5] (-1.5,-1.5) rectangle (1.5,0.5);
filldraw [canvas is yx plane at z=0.5] (-1.5,-1.5) rectangle (1.5,1.5);
foreach X in -1.5,-0.5,0.5
foreach Y in -1.5,-0.5
draw [canvas is yz plane at x=1.5,shift=(X,Y),fill=blue] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,Y),fill=red] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
tdplotsetrotatedcoords00-myPsi+90
beginscope[tdplot_rotated_coords]
foreach X in -1.5,-0.5,0.5
filldraw [canvas is yz plane at x=1.5,shift=(X,0.5)] (0,0) rectangle (1,1);
filldraw [canvas is xz plane at y=1.5,shift=(X,0.5)] (0,0) rectangle (1,1);
draw [canvas is yz plane at x=1.5,shift=(X,0.5),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,0.5),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
foreach Y in -1.5,-0.5,0.5
filldraw [canvas is yx plane at z=1.5,shift=(X,Y)] (0,0) rectangle (1,1);
draw [canvas is yx plane at z=1.5,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
endscope
endscope
endtikzpicture
enddocument
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:
documentclassarticle
usepackageanimate
usepackagetikz
usepackagetikz-3dplot
usetikzlibrary3d
newwriteOutFile%
immediateopenoutOutFile=tl.txt%
immediatewriteOutFile::0x0,1
foreach i in 2,...,36
immediatewriteOutFile::i%
immediatecloseoutOutFile
pgfmathsetmacroradius0.1
tdplotsetmaincoords70100
newcommanddrawRotatedRow[2][2]
pgfmathsetmacromyHeight-1.5+int(#1)
pgfmathsetmacromyPsi#2
pgfmathsetmacromySecondPsi-80+Mod(myPsi+80,90)
pgfmathtruncatemacromySegmentMod((myPsi+80)/90,4)
ifcasemySegment% segment 0
deffrontcolorred
defsidecolorblue
or% segment 1
deffrontcolorblue
defsidecolororange
or% segment 2
deffrontcolororange
defsidecolorwhite
or% segment 3
deffrontcolorwhite
defsidecolorred
fi
beginscope[tdplot_main_coords]
tdplotsetrotatedcoords00mySecondPsi
beginscope[tdplot_rotated_coords]
filldraw [canvas is yx plane at z=myHeight+1] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yz plane at x=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
filldraw [canvas is xz plane at y=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
foreach X in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,myHeight),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,myHeight),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
ifnum#1=2relax
foreach Y in -1.5,-0.5,0.5
draw [canvas is yx plane at z=myHeight+1,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
fi
endscope
endscope
begindocument
beginanimateinline[controls,loop,timeline=tl.txt]10
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
drawRotatedRow[0]0
drawRotatedRow[1]0
endtikzpicture
newframe
multiframe36iPsi=0+10%
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
drawRotatedRowiPsi
endtikzpicture
endanimateinline
enddocument
I added a command that draws a row of cubes, with optional z
level (defaults to 2, zero based) and with a rotation about z
: drawRotatedRow[<level>]<rotation>
. With this command now we can do something like this:
documentclass[tikz]standalone
usepackageanimate
usepackagetikz
usepackagetikz-3dplot
usetikzlibrary3d
pgfmathsetmacroradius0.1
tdplotsetmaincoords70100
newcommanddrawRotatedRow[2]
pgfmathsetmacromyHeight-1.5+int(#1)
pgfmathsetmacromyPsi#2
pgfmathsetmacromySecondPsi-80+Mod(myPsi+80,90)
pgfmathtruncatemacromySegmentMod((myPsi+80)/90,4)
ifcasemySegment% segment 0
deffrontcolorred
defsidecolorblue
or% segment 1
deffrontcolorblue
defsidecolororange
or% segment 2
deffrontcolororange
defsidecolorwhite
or% segment 3
deffrontcolorwhite
defsidecolorred
fi
beginscope[tdplot_main_coords]
tdplotsetrotatedcoords00mySecondPsi
beginscope[tdplot_rotated_coords]
filldraw [canvas is yx plane at z=myHeight+1] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yz plane at x=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
filldraw [canvas is xz plane at y=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
foreach X in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,myHeight),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,myHeight),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
ifnum#1=2relax
foreach Y in -1.5,-0.5,0.5
draw [canvas is yx plane at z=myHeight+1,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
fi
endscope
endscope
begindocument
foreach iPsi in 0,10,...,359
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
drawRotatedRow0-iPsi
drawRotatedRow10
drawRotatedRow2iPsi
endtikzpicture
enddocument
Or even this (very long GIF):
documentclass[tikz]standalone
usepackageanimate
usepackagetikz
usepackagetikz-3dplot
usetikzlibrary3d
pgfmathsetmacroradius0.1
tdplotsetmaincoords70100
newcommanddrawRotatedRow[2]
pgfmathsetmacromyHeight-1.5+int(#1)
pgfmathsetmacromyPsi#2
pgfmathsetmacromySecondPsi-80+Mod(myPsi+80,90)
pgfmathtruncatemacromySegmentMod((myPsi+80)/90,4)
ifcasemySegment% segment 0
deffrontcolorred
defsidecolorblue
or% segment 1
deffrontcolorblue
defsidecolororange
or% segment 2
deffrontcolororange
defsidecolorwhite
or% segment 3
deffrontcolorwhite
defsidecolorred
fi
beginscope[tdplot_main_coords]
tdplotsetrotatedcoords00mySecondPsi
beginscope[tdplot_rotated_coords]
filldraw [canvas is yx plane at z=myHeight+1] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yz plane at x=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
filldraw [canvas is xz plane at y=1.5] (-1.5,myHeight) rectangle (1.5,myHeight+1);
foreach X in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,myHeight),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,myHeight),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
ifnum#1=2relax
foreach Y in -1.5,-0.5,0.5
draw [canvas is yx plane at z=myHeight+1,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
fi
endscope
endscope
begindocument
foreach level in 0,1,2
foreach iPsi in 0,10,...,359
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
ifcaselevel % Level 0 rotating
drawRotatedRow0iPsi
drawRotatedRow10
drawRotatedRow20
or % Level 1 rotating
drawRotatedRow00
drawRotatedRow1iPsi
drawRotatedRow20
or % Level 2 rotating
drawRotatedRow00
drawRotatedRow10
drawRotatedRow2iPsi
fi
endtikzpicture
enddocument
edited 2 days ago
answered Aug 6 at 11:08
Max Snippe
3,81311221
3,81311221
@Max_Snippe: Nice, that's it almost; how can I adjust the view for any angle ÃÂ, àand ò of rotation?
â current_user
Aug 6 at 11:12
1
@current_user you can either use thetikz-3dplot
nativetdplotsetrotatedcoords
command, or have a look at this question and its answers.
â Max Snippe
Aug 6 at 11:13
@Max_Snippe: WOW ⦠this looks amazing! Is there a way that, for example, one row can be rotated 90 degrees and back (threaded in the animation)?
â current_user
Aug 6 at 15:24
1
@current_user it's possible, but it gets quite hard quite fast. I don't think it is possible to automate the rotation of arbitrary rows and columns.
â Max Snippe
Aug 6 at 16:10
1
@current_user See my latest edit, I believe that it might be possible to expand this to rotating columns, but I will not try to do that. I leave some work for you :)
â Max Snippe
Aug 6 at 19:47
 |Â
show 1 more comment
@Max_Snippe: Nice, that's it almost; how can I adjust the view for any angle ÃÂ, àand ò of rotation?
â current_user
Aug 6 at 11:12
1
@current_user you can either use thetikz-3dplot
nativetdplotsetrotatedcoords
command, or have a look at this question and its answers.
â Max Snippe
Aug 6 at 11:13
@Max_Snippe: WOW ⦠this looks amazing! Is there a way that, for example, one row can be rotated 90 degrees and back (threaded in the animation)?
â current_user
Aug 6 at 15:24
1
@current_user it's possible, but it gets quite hard quite fast. I don't think it is possible to automate the rotation of arbitrary rows and columns.
â Max Snippe
Aug 6 at 16:10
1
@current_user See my latest edit, I believe that it might be possible to expand this to rotating columns, but I will not try to do that. I leave some work for you :)
â Max Snippe
Aug 6 at 19:47
@Max_Snippe: Nice, that's it almost; how can I adjust the view for any angle ÃÂ, àand ò of rotation?
â current_user
Aug 6 at 11:12
@Max_Snippe: Nice, that's it almost; how can I adjust the view for any angle ÃÂ, àand ò of rotation?
â current_user
Aug 6 at 11:12
1
1
@current_user you can either use the
tikz-3dplot
native tdplotsetrotatedcoords
command, or have a look at this question and its answers.â Max Snippe
Aug 6 at 11:13
@current_user you can either use the
tikz-3dplot
native tdplotsetrotatedcoords
command, or have a look at this question and its answers.â Max Snippe
Aug 6 at 11:13
@Max_Snippe: WOW ⦠this looks amazing! Is there a way that, for example, one row can be rotated 90 degrees and back (threaded in the animation)?
â current_user
Aug 6 at 15:24
@Max_Snippe: WOW ⦠this looks amazing! Is there a way that, for example, one row can be rotated 90 degrees and back (threaded in the animation)?
â current_user
Aug 6 at 15:24
1
1
@current_user it's possible, but it gets quite hard quite fast. I don't think it is possible to automate the rotation of arbitrary rows and columns.
â Max Snippe
Aug 6 at 16:10
@current_user it's possible, but it gets quite hard quite fast. I don't think it is possible to automate the rotation of arbitrary rows and columns.
â Max Snippe
Aug 6 at 16:10
1
1
@current_user See my latest edit, I believe that it might be possible to expand this to rotating columns, but I will not try to do that. I leave some work for you :)
â Max Snippe
Aug 6 at 19:47
@current_user See my latest edit, I believe that it might be possible to expand this to rotating columns, but I will not try to do that. I leave some work for you :)
â Max Snippe
Aug 6 at 19:47
 |Â
show 1 more comment
up vote
19
down vote
Just for completeness. As there was the question about pgfplots, I just spell out Max Snippe's comment.
documentclass[border=3.14mm,tikz]standalone
usepackagepgfplots
pgfplotssetcompat=1.16
begindocument
defamax4 %number of squares in each direction
foreach X in 30
begintikzpicture
%path[use as bounding box] (-5,5) rectangle (5,5);
beginaxis[height=5cm,unit vector ratio=1 1 1,view=X20,colormap/hot,
set layers=standard,
domain=0:amax+1,
domain y=0:amax+1,
samples y=1,
xmin=-1,ymax=amax+1,
hide axis,
xtick=empty, ytick=empty, ztick=empty,
clip=false,samples=amax+1,samples y=amax+1
]
addplot3[point meta=rand,mesh,surf] (x,y,amax+1);
addplot3[point meta=rand,mesh,surf] (x,0,y);
addplot3[point meta=rand,mesh,surf] (amax+1,x,y);
endaxis
endtikzpicture
enddocument
Note:
I was not able to do a proper animation. For some reason the plots got doubled on each slide. I have no idea what's going on, and I was able to do proper pgfplots animations in the past. Most likely I am doing something really dumb.I was really dumb. For another question I changed the viewers preference to two page view. So one could do animations but compared to Max Snippe's result the outcome will be poor.If you give me rough idea what the colors should be I will be happy to add them as well. Ideally this would be some cool formula such that one could useI added random colors from a colormap that describes the temperatures these days. If I was not hibernating in winter, I could change it topoint meta
for that, otherwise I guess one has to resort to tables.cool
then.
ADDENDUM: All credits go to current_user, who had the idea, Max Snippe, who made the superb code (which I just stole), and samcarter, the author of tikzmarmots
. ;-)
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usepackagetikzmarmots
newsaveboxMarmot
saveboxMarmottikz[scale=0.4]marmot[whiskers,teeth]
usetikzlibrary3d
begindocument
pgfmathsetmacroradius0.1
foreach frontcolor [remember=frontcolor as sidecolor (initially blue)] in red,white,orange,blue
foreach myPsi in 90,100,...,170 %
tdplotsetmaincoords70myPsi
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
beginscope[tdplot_main_coords]
filldraw [canvas is yz plane at x=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is xz plane at y=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yx plane at z=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
foreach X in -1.5,-0.5,0.5
foreach Y in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,Y),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,Y),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is yx plane at z=1.5,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
beginscope[canvas is yz plane at x=1.5,transform shape]
node at (X+0.5,Y+0.5) useboxMarmot;
endscope
beginscope[canvas is xz plane at y=1.5,transform shape]
node at (X+0.5,Y+0.5) useboxMarmot;
endscope
beginscope[canvas is yx plane at z=1.5,transform shape]
node[yscale=-1] at (X+0.5,Y+0.5) useboxMarmot;
endscope
endscope
endtikzpicture
enddocument
Barbara Beeton discovered that the marmots are wagging their tails. This is because Max Snippe's routines are automatically such that the tails of the marmots are always behind the marmot. There is a reflection at the right moment. Therefore one may want to promote the marmots to 3D. (I am very optimistic that the tikzmarmots
package will provide 3D marmots in the near future. The impatient users may get the following animation by replacing all fill[
commands by shade[ball color=
in that package and modify the last marmot node to node[yscale=-1,rotate=myPsi-90] at (X+0.5,Y+0.5) useboxMarmot;
.)
1
This is great! Shame that you already had my +1
â Max Snippe
Aug 6 at 18:07
2
+1, for the turn of the tails from six to nine o'clock and the reset on the green side
â J Leon V.
Aug 6 at 23:32
1
@JLeonV. Thanks. That has already lead to some amusement in the chat ;-) But actually I didn't know about it before barbara beeton pointed it out, meaning that the projection by the 3d library does it on its own ... ;-) The reset is due to Max Snippe!
â marmot
Aug 6 at 23:35
2
@current_user Now I understand why that happens. Max Snippe's routine is more tricky than I thought. To understand this, you need to replace the marmots on the sides by different letters in the first two scopes.
â marmot
Aug 7 at 0:08
1
@marmot: As Max Snippet already mentioned: The drawing order is very important ⦠;)
â current_user
Aug 7 at 0:10
 |Â
show 7 more comments
up vote
19
down vote
Just for completeness. As there was the question about pgfplots, I just spell out Max Snippe's comment.
documentclass[border=3.14mm,tikz]standalone
usepackagepgfplots
pgfplotssetcompat=1.16
begindocument
defamax4 %number of squares in each direction
foreach X in 30
begintikzpicture
%path[use as bounding box] (-5,5) rectangle (5,5);
beginaxis[height=5cm,unit vector ratio=1 1 1,view=X20,colormap/hot,
set layers=standard,
domain=0:amax+1,
domain y=0:amax+1,
samples y=1,
xmin=-1,ymax=amax+1,
hide axis,
xtick=empty, ytick=empty, ztick=empty,
clip=false,samples=amax+1,samples y=amax+1
]
addplot3[point meta=rand,mesh,surf] (x,y,amax+1);
addplot3[point meta=rand,mesh,surf] (x,0,y);
addplot3[point meta=rand,mesh,surf] (amax+1,x,y);
endaxis
endtikzpicture
enddocument
Note:
I was not able to do a proper animation. For some reason the plots got doubled on each slide. I have no idea what's going on, and I was able to do proper pgfplots animations in the past. Most likely I am doing something really dumb.I was really dumb. For another question I changed the viewers preference to two page view. So one could do animations but compared to Max Snippe's result the outcome will be poor.If you give me rough idea what the colors should be I will be happy to add them as well. Ideally this would be some cool formula such that one could useI added random colors from a colormap that describes the temperatures these days. If I was not hibernating in winter, I could change it topoint meta
for that, otherwise I guess one has to resort to tables.cool
then.
ADDENDUM: All credits go to current_user, who had the idea, Max Snippe, who made the superb code (which I just stole), and samcarter, the author of tikzmarmots
. ;-)
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usepackagetikzmarmots
newsaveboxMarmot
saveboxMarmottikz[scale=0.4]marmot[whiskers,teeth]
usetikzlibrary3d
begindocument
pgfmathsetmacroradius0.1
foreach frontcolor [remember=frontcolor as sidecolor (initially blue)] in red,white,orange,blue
foreach myPsi in 90,100,...,170 %
tdplotsetmaincoords70myPsi
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
beginscope[tdplot_main_coords]
filldraw [canvas is yz plane at x=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is xz plane at y=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yx plane at z=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
foreach X in -1.5,-0.5,0.5
foreach Y in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,Y),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,Y),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is yx plane at z=1.5,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
beginscope[canvas is yz plane at x=1.5,transform shape]
node at (X+0.5,Y+0.5) useboxMarmot;
endscope
beginscope[canvas is xz plane at y=1.5,transform shape]
node at (X+0.5,Y+0.5) useboxMarmot;
endscope
beginscope[canvas is yx plane at z=1.5,transform shape]
node[yscale=-1] at (X+0.5,Y+0.5) useboxMarmot;
endscope
endscope
endtikzpicture
enddocument
Barbara Beeton discovered that the marmots are wagging their tails. This is because Max Snippe's routines are automatically such that the tails of the marmots are always behind the marmot. There is a reflection at the right moment. Therefore one may want to promote the marmots to 3D. (I am very optimistic that the tikzmarmots
package will provide 3D marmots in the near future. The impatient users may get the following animation by replacing all fill[
commands by shade[ball color=
in that package and modify the last marmot node to node[yscale=-1,rotate=myPsi-90] at (X+0.5,Y+0.5) useboxMarmot;
.)
1
This is great! Shame that you already had my +1
â Max Snippe
Aug 6 at 18:07
2
+1, for the turn of the tails from six to nine o'clock and the reset on the green side
â J Leon V.
Aug 6 at 23:32
1
@JLeonV. Thanks. That has already lead to some amusement in the chat ;-) But actually I didn't know about it before barbara beeton pointed it out, meaning that the projection by the 3d library does it on its own ... ;-) The reset is due to Max Snippe!
â marmot
Aug 6 at 23:35
2
@current_user Now I understand why that happens. Max Snippe's routine is more tricky than I thought. To understand this, you need to replace the marmots on the sides by different letters in the first two scopes.
â marmot
Aug 7 at 0:08
1
@marmot: As Max Snippet already mentioned: The drawing order is very important ⦠;)
â current_user
Aug 7 at 0:10
 |Â
show 7 more comments
up vote
19
down vote
up vote
19
down vote
Just for completeness. As there was the question about pgfplots, I just spell out Max Snippe's comment.
documentclass[border=3.14mm,tikz]standalone
usepackagepgfplots
pgfplotssetcompat=1.16
begindocument
defamax4 %number of squares in each direction
foreach X in 30
begintikzpicture
%path[use as bounding box] (-5,5) rectangle (5,5);
beginaxis[height=5cm,unit vector ratio=1 1 1,view=X20,colormap/hot,
set layers=standard,
domain=0:amax+1,
domain y=0:amax+1,
samples y=1,
xmin=-1,ymax=amax+1,
hide axis,
xtick=empty, ytick=empty, ztick=empty,
clip=false,samples=amax+1,samples y=amax+1
]
addplot3[point meta=rand,mesh,surf] (x,y,amax+1);
addplot3[point meta=rand,mesh,surf] (x,0,y);
addplot3[point meta=rand,mesh,surf] (amax+1,x,y);
endaxis
endtikzpicture
enddocument
Note:
I was not able to do a proper animation. For some reason the plots got doubled on each slide. I have no idea what's going on, and I was able to do proper pgfplots animations in the past. Most likely I am doing something really dumb.I was really dumb. For another question I changed the viewers preference to two page view. So one could do animations but compared to Max Snippe's result the outcome will be poor.If you give me rough idea what the colors should be I will be happy to add them as well. Ideally this would be some cool formula such that one could useI added random colors from a colormap that describes the temperatures these days. If I was not hibernating in winter, I could change it topoint meta
for that, otherwise I guess one has to resort to tables.cool
then.
ADDENDUM: All credits go to current_user, who had the idea, Max Snippe, who made the superb code (which I just stole), and samcarter, the author of tikzmarmots
. ;-)
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usepackagetikzmarmots
newsaveboxMarmot
saveboxMarmottikz[scale=0.4]marmot[whiskers,teeth]
usetikzlibrary3d
begindocument
pgfmathsetmacroradius0.1
foreach frontcolor [remember=frontcolor as sidecolor (initially blue)] in red,white,orange,blue
foreach myPsi in 90,100,...,170 %
tdplotsetmaincoords70myPsi
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
beginscope[tdplot_main_coords]
filldraw [canvas is yz plane at x=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is xz plane at y=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yx plane at z=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
foreach X in -1.5,-0.5,0.5
foreach Y in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,Y),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,Y),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is yx plane at z=1.5,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
beginscope[canvas is yz plane at x=1.5,transform shape]
node at (X+0.5,Y+0.5) useboxMarmot;
endscope
beginscope[canvas is xz plane at y=1.5,transform shape]
node at (X+0.5,Y+0.5) useboxMarmot;
endscope
beginscope[canvas is yx plane at z=1.5,transform shape]
node[yscale=-1] at (X+0.5,Y+0.5) useboxMarmot;
endscope
endscope
endtikzpicture
enddocument
Barbara Beeton discovered that the marmots are wagging their tails. This is because Max Snippe's routines are automatically such that the tails of the marmots are always behind the marmot. There is a reflection at the right moment. Therefore one may want to promote the marmots to 3D. (I am very optimistic that the tikzmarmots
package will provide 3D marmots in the near future. The impatient users may get the following animation by replacing all fill[
commands by shade[ball color=
in that package and modify the last marmot node to node[yscale=-1,rotate=myPsi-90] at (X+0.5,Y+0.5) useboxMarmot;
.)
Just for completeness. As there was the question about pgfplots, I just spell out Max Snippe's comment.
documentclass[border=3.14mm,tikz]standalone
usepackagepgfplots
pgfplotssetcompat=1.16
begindocument
defamax4 %number of squares in each direction
foreach X in 30
begintikzpicture
%path[use as bounding box] (-5,5) rectangle (5,5);
beginaxis[height=5cm,unit vector ratio=1 1 1,view=X20,colormap/hot,
set layers=standard,
domain=0:amax+1,
domain y=0:amax+1,
samples y=1,
xmin=-1,ymax=amax+1,
hide axis,
xtick=empty, ytick=empty, ztick=empty,
clip=false,samples=amax+1,samples y=amax+1
]
addplot3[point meta=rand,mesh,surf] (x,y,amax+1);
addplot3[point meta=rand,mesh,surf] (x,0,y);
addplot3[point meta=rand,mesh,surf] (amax+1,x,y);
endaxis
endtikzpicture
enddocument
Note:
I was not able to do a proper animation. For some reason the plots got doubled on each slide. I have no idea what's going on, and I was able to do proper pgfplots animations in the past. Most likely I am doing something really dumb.I was really dumb. For another question I changed the viewers preference to two page view. So one could do animations but compared to Max Snippe's result the outcome will be poor.If you give me rough idea what the colors should be I will be happy to add them as well. Ideally this would be some cool formula such that one could useI added random colors from a colormap that describes the temperatures these days. If I was not hibernating in winter, I could change it topoint meta
for that, otherwise I guess one has to resort to tables.cool
then.
ADDENDUM: All credits go to current_user, who had the idea, Max Snippe, who made the superb code (which I just stole), and samcarter, the author of tikzmarmots
. ;-)
documentclass[border=5pt,tikz]standalone
usepackagetikz-3dplot
usepackagetikzmarmots
newsaveboxMarmot
saveboxMarmottikz[scale=0.4]marmot[whiskers,teeth]
usetikzlibrary3d
begindocument
pgfmathsetmacroradius0.1
foreach frontcolor [remember=frontcolor as sidecolor (initially blue)] in red,white,orange,blue
foreach myPsi in 90,100,...,170 %
tdplotsetmaincoords70myPsi
begintikzpicture[line join=round]
clip (-3,-2.5) rectangle (3,2.5);
beginscope[tdplot_main_coords]
filldraw [canvas is yz plane at x=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is xz plane at y=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
filldraw [canvas is yx plane at z=1.5] (-1.5,-1.5) rectangle (1.5,1.5);
foreach X in -1.5,-0.5,0.5
foreach Y in -1.5,-0.5,0.5
draw [canvas is yz plane at x=1.5,shift=(X,Y),fill=sidecolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is xz plane at y=1.5,shift=(X,Y),fill=frontcolor] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
draw [canvas is yx plane at z=1.5,shift=(X,Y),fill=green!60!black] (0.5,0) -- (1-radius,0) arc (-90:0:radius) -- (1,1-radius) arc (0:90:radius) -- (radius,1) arc (90:180:radius) -- (0,radius) arc (180:270:radius) -- cycle;
beginscope[canvas is yz plane at x=1.5,transform shape]
node at (X+0.5,Y+0.5) useboxMarmot;
endscope
beginscope[canvas is xz plane at y=1.5,transform shape]
node at (X+0.5,Y+0.5) useboxMarmot;
endscope
beginscope[canvas is yx plane at z=1.5,transform shape]
node[yscale=-1] at (X+0.5,Y+0.5) useboxMarmot;
endscope
endscope
endtikzpicture
enddocument
Barbara Beeton discovered that the marmots are wagging their tails. This is because Max Snippe's routines are automatically such that the tails of the marmots are always behind the marmot. There is a reflection at the right moment. Therefore one may want to promote the marmots to 3D. (I am very optimistic that the tikzmarmots
package will provide 3D marmots in the near future. The impatient users may get the following animation by replacing all fill[
commands by shade[ball color=
in that package and modify the last marmot node to node[yscale=-1,rotate=myPsi-90] at (X+0.5,Y+0.5) useboxMarmot;
.)
edited Aug 7 at 0:23
answered Aug 6 at 14:55
marmot
48.6k34396
48.6k34396
1
This is great! Shame that you already had my +1
â Max Snippe
Aug 6 at 18:07
2
+1, for the turn of the tails from six to nine o'clock and the reset on the green side
â J Leon V.
Aug 6 at 23:32
1
@JLeonV. Thanks. That has already lead to some amusement in the chat ;-) But actually I didn't know about it before barbara beeton pointed it out, meaning that the projection by the 3d library does it on its own ... ;-) The reset is due to Max Snippe!
â marmot
Aug 6 at 23:35
2
@current_user Now I understand why that happens. Max Snippe's routine is more tricky than I thought. To understand this, you need to replace the marmots on the sides by different letters in the first two scopes.
â marmot
Aug 7 at 0:08
1
@marmot: As Max Snippet already mentioned: The drawing order is very important ⦠;)
â current_user
Aug 7 at 0:10
 |Â
show 7 more comments
1
This is great! Shame that you already had my +1
â Max Snippe
Aug 6 at 18:07
2
+1, for the turn of the tails from six to nine o'clock and the reset on the green side
â J Leon V.
Aug 6 at 23:32
1
@JLeonV. Thanks. That has already lead to some amusement in the chat ;-) But actually I didn't know about it before barbara beeton pointed it out, meaning that the projection by the 3d library does it on its own ... ;-) The reset is due to Max Snippe!
â marmot
Aug 6 at 23:35
2
@current_user Now I understand why that happens. Max Snippe's routine is more tricky than I thought. To understand this, you need to replace the marmots on the sides by different letters in the first two scopes.
â marmot
Aug 7 at 0:08
1
@marmot: As Max Snippet already mentioned: The drawing order is very important ⦠;)
â current_user
Aug 7 at 0:10
1
1
This is great! Shame that you already had my +1
â Max Snippe
Aug 6 at 18:07
This is great! Shame that you already had my +1
â Max Snippe
Aug 6 at 18:07
2
2
+1, for the turn of the tails from six to nine o'clock and the reset on the green side
â J Leon V.
Aug 6 at 23:32
+1, for the turn of the tails from six to nine o'clock and the reset on the green side
â J Leon V.
Aug 6 at 23:32
1
1
@JLeonV. Thanks. That has already lead to some amusement in the chat ;-) But actually I didn't know about it before barbara beeton pointed it out, meaning that the projection by the 3d library does it on its own ... ;-) The reset is due to Max Snippe!
â marmot
Aug 6 at 23:35
@JLeonV. Thanks. That has already lead to some amusement in the chat ;-) But actually I didn't know about it before barbara beeton pointed it out, meaning that the projection by the 3d library does it on its own ... ;-) The reset is due to Max Snippe!
â marmot
Aug 6 at 23:35
2
2
@current_user Now I understand why that happens. Max Snippe's routine is more tricky than I thought. To understand this, you need to replace the marmots on the sides by different letters in the first two scopes.
â marmot
Aug 7 at 0:08
@current_user Now I understand why that happens. Max Snippe's routine is more tricky than I thought. To understand this, you need to replace the marmots on the sides by different letters in the first two scopes.
â marmot
Aug 7 at 0:08
1
1
@marmot: As Max Snippet already mentioned: The drawing order is very important ⦠;)
â current_user
Aug 7 at 0:10
@marmot: As Max Snippet already mentioned: The drawing order is very important ⦠;)
â current_user
Aug 7 at 0:10
 |Â
show 7 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f444812%2fplaying-around-with-a-rubiks-cube-in-tikz%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
4
For your first question, I think you should look at the
tikz-3dplot
package. Thetdplotsetmaincoords
command is what you need I think. Also have a look at this question.â Max Snippe
Aug 6 at 10:51
@Max_Snippe: No, this isn't what I was looking for â I want to (1) to get a view which can be change by two parameters and (2) get TikZ to scramble the cube, but thank you anyway.
â current_user
Aug 6 at 10:53
2
Damn I really don't have the time to write elaborate answers but you keep testing my procrastination 'skills' with these cool questions.
â Max Snippe
Aug 6 at 11:10
2
Do you know the Rubik cube packages ctan.org/search/?phrase=rubik ? Maybe they could be used to simply the display etc.
â samcarter
Aug 6 at 11:11
1
Note that randomizing the colors is not easy if you want to guarantee a solvable cube. Only showing 3 sides makes it easier, but still not trivial.
â Matt
Aug 6 at 15:21