5. Rubik's Cube: PlotRubiksCube パッケージ

目次へ

ルービックキューブの図を描いてチェックすることが多いので PlotRubiksCube パッケージを作ることにする。

手続き

  • 後に載せるプログラムのファイル名を PlotRubiksCube.m とする。
  • ノートブックの作業ディレクトリに PlotRubiksCube.m を置く。

 使用例のノートブック

SetDirectory[NotebookDirectory[]];
<< PlotRubiksCube.m

Names["PlotRubiksCube`*"]
⇒ {"PlotCube", "PlotCube3D"}

?PlotCube
⇒ PlotCube[status] plots 2D map of Rubik's cube. status is a permutation list.

p0 = Range[48];
Show[Rasterize[PlotCube[p0]], ImageSize -> 400]
?PlotCube3D
⇒ PlotCube3D[psi] plots a Rubik's Cube. psi is a permutation list.

Show[PlotCube3D[p0], ImageSize -> 200]

PlotRubiksCube.m

(* ::Package:: *)
BeginPackage["PlotRubiksCube`"];

PlotCube::usage="PlotCube[status] plots 2D map of Rubik's cube. status is a permutation list.";
PlotCube3D::usage="PlotCube3D[psi] plots a Rubik's Cube. psi is a permutation list.";

Begin["`Private`"];

(*  ---- plotCube ---- *)
(*  Auxiliary function  *)
statusForPlot[status_,face_]:=Module[{blanks,aa,bb,cc,up,leftFrontRightBack,down},
  blanks=Table["",{3}];
  aa=Partition[status,8];
  bb=MapThread[Insert[#1,#2,5]&,{aa,face}];
  cc=Partition[#,3]&/@bb;
  up=Table[Join[blanks,cc[[1,i]],blanks,blanks],{i,1,3}];
  leftFrontRightBack=Table[Join[cc[[2,j]],cc[[3,j]],cc[[4,j]],cc[[5,j]]],{j,1,3}];
  down=Table[Join[blanks,cc[[6,i]],blanks,blanks],{i,1,3}];
  Join[up,leftFrontRightBack,down]
]
(*   *)
PlotCube[status_] := Module[{face,
  upFrame=Flatten[Table[{{i,i},{j,j}}->True,{i,1,3},{j,4,6}],1],
  leftFrame=Flatten[Table[{{i,i},{j,j}}->True,{i,4,6},{j,1,3}],1],
  frontFrame=Flatten[Table[{{i,i},{j,j}}->True,{i,4,6},{j,4,6}],1],
  rightFrame=Flatten[Table[{{i,i},{j,j}}->True,{i,4,6},{j,7,9}],1],
  backFrame=Flatten[Table[{{i,i},{j,j}}->True,{i,4,6},{j,10,12}],1],
  downFrame=Flatten[Table[{{i,i},{j,j}}->True,{i,7,9},{j,4,6}],1],
  faceColor,upColor,leftColor,frontColor,rightColor,backColor,downColor,ss},
  (*  *)
  face=Style[#,Blue,Bold,16]&/@{"U","L","F","R","B","D"};
  (*    *)
  ss = statusForPlot[status,face];
  (*   color  *)
  faceColor={{2,5}->Orange,{5,2}->Lighter[Blue,0.6],{5,5}->Green,{5,8}->White,{5,11}->Yellow,{8,5}->Lighter[Red,0.2]};
  (*    *)
     upColor=Map[#->Orange&,Flatten[Position[ss,#]&/@Range[8],1]];
    leftColor=Map[#->Lighter[Blue,0.6]&,Flatten[Position[ss,#]&/@Range[9,16],1]];
   frontColor=Map[#->Green&,Flatten[Position[ss,#]&/@Range[17,24],1]];
   rightColor=Map[#->White&,Flatten[Position[ss,#]&/@Range[25,32],1]];
    backColor=Map[#->Lighter[Yellow,0.1]&,Flatten[Position[ss,#]&/@Range[33,40],1]];
    downColor=Map[#->Lighter[Red,0.2]&,Flatten[Position[ss,#]&/@Range[41,48],1]]; 
  (*  -----   *)
  Grid[ ss, ItemSize->{1.5,1.5},
    Frame->{None,None,Join[upFrame,leftFrame,frontFrame,rightFrame,backFrame,downFrame]},
    Spacings->{1.5,2},
    Background->{None,None,Join[faceColor,upColor,leftColor,frontColor,rightColor,backColor,downColor]}
  ]
]
(*  --- PlotCube3D  ---- *)
edge=EdgeForm[Directive[Thickness[0.01],Black]];
color={"Orange","Blue","Green","White","Yellow","Red"};
fcolor[n_] := Which[
  1<=n<=8,Orange,
  9<=n<=16,Blue,
  17<=n<=24,Green,
  25<=n<=32,White,
  33<=n<=40,Yellow,
  41<=n<=48,Red,True,"error"]
(* 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,2vyU}+{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,Opacity[0.6],face[1],Polygon[f01]},Text[num[1],center01]},
{{edge,Opacity[0.6],face[2],Polygon[f02]},Text[num[2],center02]},
{{edge,Opacity[0.6],face[3],Polygon[f03]},Text[num[3],center03]},
{{edge,Opacity[0.6],face[4],Polygon[f04]},Text[num[4],center04]},
{{edge,Opacity[0.6],Orange,Polygon[fUp]},Red,Style[Text["U",centerUp],16,Bold,FontFamily->"Courier"]},
{{edge,Opacity[0.6],face[5],Polygon[f05]},Text[num[5],center05]},
{{edge,Opacity[0.6],face[6],Polygon[f06]},Text[num[6],center06]},
{{edge,Opacity[0.6],face[7],Polygon[f07]},Text[num[7],center07]},
{{edge,Opacity[0.6],face[8],Polygon[f08]},Text[num[8],center08]},
(* ---  Front ---  *)
{{edge,Opacity[0.6],face[17],Polygon[f17]},Text[num[17],center17]},
{{edge,Opacity[0.6],face[18],Polygon[f18]},Text[num[18],center18]},
{{edge,Opacity[0.6],face[19],Polygon[f19]},Text[num[19],center19]},
{{edge,Opacity[0.6],face[20],Polygon[f20]},Text[num[20],center20]},
{{edge,Opacity[0.6],Green,Polygon[fF]},Red,Style[Text["F",centerF],16,Bold,FontFamily->"Courier"]},
{{edge,Opacity[0.6],face[21],Polygon[f21]},Text[num[21],center21]},
{{edge,Opacity[0.6],face[22],Polygon[f22]},Text[num[22],center22]},
{{edge,Opacity[0.6],face[23],Polygon[f23]},Text[num[23],center23]},
{{edge,Opacity[0.6],face[24],Polygon[f24]},Text[num[24],center24]},
(* --- Right --- *)
{{edge,Opacity[0.6],face[25],Polygon[f25]},Text[num[25],center25]},
{{edge,Opacity[0.6],face[26],Polygon[f26]},Text[num[26],center26]},
{{edge,Opacity[0.6],face[27],Polygon[f27]},Text[num[27],center27]},
{{edge,Opacity[0.6],face[28],Polygon[f28]},Text[num[28],center28]},
{{edge,Opacity[0.6],White,Polygon[fR]},Red,Style[Text["R",centerR],16,Bold,FontFamily->"Courier"]},
{{edge,Opacity[0.6],face[29],Polygon[f29]},Text[num[29],center29]},
{{edge,Opacity[0.6],face[30],Polygon[f30]},Text[num[30],center30]},
{{edge,Opacity[0.6],face[31],Polygon[f31]},Text[num[31],center31]},
{{edge,Opacity[0.6],face[32],Polygon[f32]},Text[num[32],center32]}
},
Axes->False,ImageSize->250]/.rule
]
End[];
EndPackage[]; 

目次へ