ルービックキューブの展開図を 2. で描いたので、次に立体図を描くコードを作った。
この立体図の配置リストは次で、これを入力データとして関数 PlotCube3D を使って描いた。
Needs["PlotRubiksCube`"]
p0= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48};
PlotCube3D[ p0 ]
関数 PlotCube3D
Polygon を使って小面を描いている。また多数の変数を使っているので、他で定義した変数と衝突しないようにパッケージの機能を使っている。
BeginPackage["PlotRubiksCube`"];
PlotCube3D::usage = "PlotCube3D[psi] plots a Rubik's Cube using psi.";
Begin["`Private`"];
fcolor[n_] := Which[
1 <= n <= 8, Lighter[Orange, 0.2],
9 <= n <= 16, Lighter[Blue, 0.5],
17 <= n <= 24, Darker[Green, 0.3],
25 <= n <= 32, White,
33 <= n <= 40, Lighter[Yellow, 0.1],
41 <= n <= 48, Lighter[Red, 0.4], True, "error"]
edge = EdgeForm[Directive[Thickness[0.01], Darker[Blue, 0.5]]];
color = {"Orange", "Blue", "Green", "White", "Yellow", "Red"};
(* front *)
cubieF = (1/3)*{{0, 0}, {3.6, 0}, {3.6, 3.6}, {0, 3.6}, {0, 0}};
vxF = 3.6/3; vyF = 3.6/3;
f17 = Map[# + {0, 2*vyF} &, cubieF]; center17 = Plus @@ f17[[{1, 3}]]/2.;
f18 = Map[# + {vxF, 2*vyF} &, cubieF]; center18 = Plus @@ f18[[{1, 3}]]/2.0;
f19 = Map[# + {2*vxF, 2*vyF} &, cubieF]; center19 = Plus @@ f19[[{1, 3}]]/2.0;
f20 = Map[# + {0, vyF} &, cubieF]; center20 = Plus @@ f20[[{1, 3}]]/2.;
fF = Map[# + {vxF, vyF} &, cubieF]; centerF = Plus @@ fF[[{1, 3}]]/2.0;
f21 = Map[# + {2*vxF, vyF} &, cubieF]; center21 = Plus @@ f21[[{1, 3}]]/2.0;
f22 = cubieF; center22 = Plus @@ f22[[{1, 3}]]/2.;
f23 = Map[# + {vxF, 0} &, cubieF]; center23 = Plus @@ f23[[{1, 3}]]/2.0;
f24 = Map[# + {2*vxF, 0} &, cubieF]; center24 = Plus @@ f24[[{1, 3}]]/2.0;
(* up *)
vxU = 3.6/3; vyU = 1.8/3;
cubieUp = (1/3)*{{0, 0}, {3.6, 0}, {5.4, 1.8}, {1.8, 1.8}, {0, 0}};
pu = {0, 3.6};
f01 = Map[# + pu + {2*vyU, 2 vyU} + {0, 0} &, cubieUp]; center01 = Plus @@ f01[[{1, 3}]]/2.;
f02 = Map[# + pu + {2*vyU, 2*vyU} + {vxU, 0} &, cubieUp]; center02 = Plus @@ f02[[{1, 3}]]/2.;
f03 = Map[# + pu + {2*vyU, 2*vyU} + {2*vxU, 0} &, cubieUp]; center03 = Plus @@ f03[[{1, 3}]]/2.;
f04 = Map[# + pu + {vyU, vyU} + {0, 0} &, cubieUp]; center04 = Plus @@ f04[[{1, 3}]]/2.;
fUp = Map[# + pu + {vyU, vyU} + {vxU, 0} &, cubieUp]; centerUp = Plus @@ fUp[[{1, 3}]]/2.;
f05 = Map[# + pu + {vyU, vyU} + {2*vxU, 0} &, cubieUp]; center05 = Plus @@ f05[[{1, 3}]]/2.;
f06 = Map[# + pu &, cubieUp]; center06 = Plus @@ f06[[{1, 3}]]/2.;
f07 = Map[# + pu + {vxU, 0} &, cubieUp]; center07 = Plus @@ f07[[{1, 3}]]/2.;
f08 = Map[# + pu + {2*vxU, 0} &, cubieUp]; center08 = Plus @@ f08[[{1, 3}]]/2.;
(* right *)
cubieR = (1/3)*{{0, 0}, {1.8, 1.8}, {1.8, 1.8} + {0, 3.6}, {0, 3.6}, {0, 0}};
vxR = 1.8/3; vyR = 3.6/3;
pr = {3.6, 0};
f25 = Map[# + pr + {0, 2*vyR} &, cubieR]; center25 = Plus @@ f25[[{1, 3}]]/2.;
f26 = Map[# + pr + {vxR, vxR} + {0, 2*vyR} &, cubieR]; center26 = Plus @@ f26[[{1, 3}]]/2.;
f27 = Map[# + pr + {2*vxR, 2*vxR} + {0, 2*vyR} &, cubieR]; center27 = Plus @@ f27[[{1, 3}]]/2.;
f28 = Map[# + pr + {0, vyR} &, cubieR]; center28 = Plus @@ f28[[{1, 3}]]/2.;
fR = Map[# + pr + {vxR, vxR} + {0, vyR} &, cubieR]; centerR = Plus @@ fR[[{1, 3}]]/2.;
f29 = Map[# + pr + {2*vxR, 2*vxR} + {0, vyR} &, cubieR]; center29 = Plus @@ f29[[{1, 3}]]/2.;
f30 = Map[# + pr + {0, 0} &, cubieR]; center30 = Plus @@ f30[[{1, 3}]]/2.;
f31 = Map[# + pr + {vxR, vxR} + {0, 0} &, cubieR]; center31 = Plus @@ f31[[{1, 3}]]/2.;
f32 = Map[# + pr + {2*vxR, 2*vxR} + {0, 0} &, cubieR]; center32 = Plus @@ f32[[{1, 3}]]/2.;
(* function plotCube3D[ ] *)
PlotCube3D[psi_] := Module[{face, num, rule},
Clear[face, num];
rule = Flatten[Table[{face[i] -> fcolor[psi[[i]]], num[i] -> ToString[psi[[i]]]}, {i, 1, 48}]];
Graphics[{
(* --- Up --- *)
{{edge, face[1], Polygon[f01]}, Text[num[1], center01]},
{{edge, face[2], Polygon[f02]}, Text[num[2], center02]},
{{edge, face[3], Polygon[f03]}, Text[num[3], center03]},
{{edge, face[4], Polygon[f04]}, Text[num[4], center04]},
{{edge, Lighter[Orange, 0.2], Polygon[fUp]}, Red,
Style[Text["U", centerUp], 16, Bold, FontFamily -> "Courier"]},
{{edge, face[5], Polygon[f05]}, Text[num[5], center05]},
{{edge, face[6], Polygon[f06]}, Text[num[6], center06]},
{{edge, face[7], Polygon[f07]}, Text[num[7], center07]},
{{edge, face[8], Polygon[f08]}, Text[num[8], center08]},
(* --- Front --- *)
{{edge, face[17], Polygon[f17]}, Text[num[17], center17]},
{{edge, face[18], Polygon[f18]}, Text[num[18], center18]},
{{edge, face[19], Polygon[f19]}, Text[num[19], center19]},
{{edge, face[20], Polygon[f20]}, Text[num[20], center20]},
{{edge, Darker[Green, 0.3], Polygon[fF]}, Red,
Style[Text["F", centerF], 16, Bold, FontFamily -> "Courier"]},
{{edge, face[21], Polygon[f21]}, Text[num[21], center21]},
{{edge, face[22], Polygon[f22]}, Text[num[22], center22]},
{{edge, face[23], Polygon[f23]}, Text[num[23], center23]},
{{edge, face[24], Polygon[f24]}, Text[num[24], center24]},
(* --- Right --- *)
{{edge, face[25], Polygon[f25]}, Text[num[25], center25]},
{{edge, face[26], Polygon[f26]}, Text[num[26], center26]},
{{edge, face[27], Polygon[f27]}, Text[num[27], center27]},
{{edge, face[28], Polygon[f28]}, Text[num[28], center28]},
{{edge, White, Polygon[fR]}, Lighter[Red, 0.4],
Style[Text["R", centerR], 16, Bold, FontFamily -> "Courier"]},
{{edge, face[29], Polygon[f29]}, Text[num[29], center29]},
{{edge, face[30], Polygon[f30]}, Text[num[30], center30]},
{{edge, face[31], Polygon[f31]}, Text[num[31], center31]},
{{edge, face[32], Polygon[f32]}, Text[num[32], center32]}
},
Axes -> False, ImageSize -> 250] /. rule
]
End[];
EndPackage[];