# Rotations of a cube

I am trying to create a program using Python 3 which must simulate the rotations of a cube.

However, I am struggling to figure out how to rotate that cube. I have the following formulas:

def rotate_x(self, angle):
""" Rotates the point around the X axis by the given angle in degrees. """
rad = angle * math.pi / 180
y = self.y * cosa - self.z * sina
z = self.y * sina + self.z * cosa
return Point3D(self.x, y, z)

def rotate_y(self, angle):
""" Rotates the point around the Y axis by the given angle in degrees. """
rad = angle * math.pi / 180
z = self.z * cosa - self.x * sina
x = self.z * sina + self.x * cosa
return Point3D(x, self.y, z)

def rotate_z(self, angle):
""" Rotates the point around the Z axis by the given angle in degrees. """
rad = angle * math.pi / 180
x = self.x * cosa - self.y * sina
y = self.x * sina + self.y * cosa
return Point3D(x, y, self.z)


I run these functions on each vertex of the cube to rotate it. However, my problem is: how do I generate a list of all the possible rotations of the cube - rotating only by multiples of 90$^{\circ}$ or by 0$^{\circ}$ around the x, y, or z axes - while avoiding duplicates? I can rotate multiple times. I know there are 24 rotations - but what I'm wondering is how to generate them.

What I'm looking for is a list of all possible rotations (for example: 90$^{\circ}$x, 180$^{\circ}$y; 270$^{\circ}$x, 90$^{\circ}$z; 180$^{\circ}$y; etc) and an explanation of how you got to this list.

• Isn't a cube symmetric so a rotation of 90° doesn't change anything? Unless this is a die with 6 different sides. If it is, each side has four configurations for a total of 24 combinations. Sep 3, 2015 at 13:02
• @ja72 All the sides are different, and thanks for the comment but you didn't read the question. What I'm looking for is a list of all the combinations and an explanation of how they can be achieved by rotating the cube by factors of 90 degrees from a predefined starting position. Sep 3, 2015 at 20:51
• Do read about transformation matrix for 3D: en.wikipedia.org/wiki/… Sep 4, 2015 at 19:53

Such problems are tackled in group theory.

My friend Ruby gave me this list of rotations and some of the aliases she knows:

rots = [
k = 1: e=12345678 ["ee", "xxxx", "yyyy", "zzzz", "xexxx", "xxexx", "xxxex", "xxxxe", "yeyyy", "yyeyy", "yyyey", "yyyye", "zezzz", "zzezz", "zzzez", "zzzze"]
k = 2: x=56218734 ["xe", "xee", "ex", "yxz", "xeee", "yexz", "yxez", "yxze", "xeeee", "xxxxx", "xyyyy", "xzzzz", "yeexz", "yexez", "yexze", "yxeez", "yxeze", "yxxzy", "yxzee", "yyxzz", "yyyyx", "yzyyy", "zxyyy", "zyxxz", "zyzyy", "zzxyy", "zzyzy", "zzzxy", "zzzyz", "zzzzx"]
k = 3: y=41238567 ["ye", "ey", "yee", "zyx", "yeee", "zeyx", "zyex", "zyxe", "xxxxy", "xxxyz", "xxxzx", "xxyzz", "xxzxz", "xyzzz", "xzxzz", "xzyyx", "yeeee", "yxxxx", "yyyyy", "yzzzz", "zeeyx", "zeyex", "zeyxe", "zxzzz", "zyeex", "zyexe", "zyxee", "zyyxz", "zzyxx", "zzzzy"]
k = 4: z=26731584 ["ze", "ez", "xzy", "zee", "xezy", "xzey", "xzye", "zeee", "xeezy", "xezey", "xezye", "xxxxz", "xxzyy", "xyxxx", "xzeey", "xzeye", "xzyee", "xzzyx", "yxyxx", "yxzzy", "yyxyx", "yyyxy", "yyyyz", "yyyzx", "yyzxx", "yzxxx", "zeeee", "zxxxx", "zyyyy", "zzzzz"]
k = 5: xx=87654321 ["xex", "xxe", "xeex", "xexe", "exx", "xxee", "xyxz", "yxxy", "yxyz", "yxzx", "yyzz", "yzxz", "zxxz", "zzyy", "xeeex", "xeexe", "xexee", "xeyxz", "xxeee", "xyexz", "xyxez", "xyxze", "yexxy", "yexyz", "yexzx", "yeyzz", "yezxz", "yxexy", "yxeyz", "yxezx", "yxxey", "yxxye", "yxyez", "yxyze", "yxzex", "yxzxe", "yyezz", "yyzez", "yyzze", "yzexz", "yzxez", "yzxze", "zexxz", "zezyy", "zxexz", "zxxez", "zxxze", "zzeyy", "zzyey", "zzyye"]
k = 6: xy=15624873 ["yz", "zx", "xey", "xye", "yez", "yze", "zex", "zxe", "xeey", "exy", "xeye", "xyee", "xzyx", "yeez", "yeze", "yxzy", "yzee", "zeex", "zexe", "zxee", "zyxz", "xeeey", "xeeye", "xeyee", "xezyx", "xyeee", "xzeyx", "xzyex", "xzyxe", "yeeez", "yeeze", "yexzy", "yezee", "yxezy", "yxzey", "yxzye", "yzeee", "zeeex", "zeexe", "zexee", "zeyxz", "zxeee", "zyexz", "zyxez", "zyxze"]
k = 7: xz=67325841 ["xez", "xze", "xeez", "exz", "xeze", "xxzy", "xzee", "yxzz", "yyyx", "zyyy", "xeeez", "xeeze", "xexzy", "xezee", "xxezy", "xxzey", "xxzye", "xzeee", "yexzz", "yeyyx", "yxezz", "yxzez", "yxzze", "yyeyx", "yyyex", "yyyxe", "zeyyy", "zyeyy", "zyyey", "zyyye"]
k = 8: yx=85147623 ["yex", "yxe", "eyx", "xzzz", "yeex", "yexe", "yxee", "yyxz", "zyxx", "zzzy", "xezzz", "xzezz", "xzzez", "xzzze", "yeeex", "yeexe", "yexee", "yeyxz", "yxeee", "yyexz", "yyxez", "yyxze", "zeyxx", "zezzy", "zyexx", "zyxex", "zyxxe", "zzezy", "zzzey", "zzzye"]
k = 9: yy=34127856 ["yey", "yye", "eyy", "xxzz", "xyyx", "yeey", "yeye", "yyee", "yzyx", "zxyx", "zyxy", "zyyz", "zyzx", "zzxx", "xexzz", "xeyyx", "xxezz", "xxzez", "xxzze", "xyeyx", "xyyex", "xyyxe", "yeeey", "yeeye", "yeyee", "yezyx", "yyeee", "yzeyx", "yzyex", "yzyxe", "zexyx", "zeyxy", "zeyyz", "zeyzx", "zezxx", "zxeyx", "zxyex", "zxyxe", "zyexy", "zyeyz", "zyezx", "zyxey", "zyxye", "zyyez", "zyyze", "zyzex", "zyzxe", "zzexx", "zzxex", "zzxxe"]
k = 10: zy=32674158 ["zey", "zye", "ezy", "xxxz", "xzyy", "yxxx", "zeey", "zeye", "zyee", "zzyx", "xexxz", "xezyy", "xxexz", "xxxez", "xxxze", "xzeyy", "xzyey", "xzyye", "yexxx", "yxexx", "yxxex", "yxxxe", "zeeey", "zeeye", "zeyee", "zezyx", "zyeee", "zzeyx", "zzyex", "zzyxe"]
k = 11: zz=65872143 ["zez", "zze", "ezz", "xxyy", "xyzy", "xzxy", "xzyz", "xzzx", "yyxx", "yzzy", "zeez", "zeze", "zxzy", "zzee", "xexyy", "xeyzy", "xezxy", "xezyz", "xezzx", "xxeyy", "xxyey", "xxyye", "xyezy", "xyzey", "xyzye", "xzexy", "xzeyz", "xzezx", "xzxey", "xzxye", "xzyez", "xzyze", "xzzex", "xzzxe", "yeyxx", "yezzy", "yyexx", "yyxex", "yyxxe", "yzezy", "yzzey", "yzzye", "zeeez", "zeeze", "zexzy", "zezee", "zxezy", "zxzey", "zxzye", "zzeee"]
k = 12: xxx=43781265 ["xexx", "xxex", "xxxe", "xeexx", "xexex", "xexxe", "exxx", "xxeex", "xxexe", "xxxee", "xxyxz", "xyxxy", "xyxyz", "xyxzx", "xyyzz", "xyzxz", "xzxxz", "xzzyy", "yxxyx", "yxyxy", "yxyyz", "yxyzx", "yxzxx", "yyxyy", "yyyzy", "yyzxy", "yyzyz", "yyzzx", "yzxxy", "yzxyz", "yzxzx", "yzyzz", "yzzxz", "zxxxy", "zxxyz", "zxxzx", "zxyzz", "zxzxz", "zyzzz", "zzxzz", "zzyyx"]
k = 13: xxy=58761432 ["xyz", "xzx", "yzz", "zxz", "xexy", "xeyz", "xezx", "xxey", "xxye", "xyez", "xyze", "xzex", "xzxe", "yezz", "yzez", "yzze", "zexz", "zxez", "zxze", "xeexy", "xeeyz", "xeezx", "xexey", "exxy", "xexye", "xeyez", "xeyze", "xezex", "xezxe", "xxeey", "xxeye", "xxyee", "xxzyx", "xyeez", "xyeze", "xyxzy", "xyzee", "xzeex", "xzexe", "xzxee", "xzyxz", "yeezz", "yezez", "yezze", "yxxyy", "yxyzy", "yxzxy", "yxzyz", "yxzzx", "yyyxx", "yyzzy", "yzeez", "yzeze", "yzxzy", "yzzee", "zeexz", "zexez", "zexze", "zxeez", "zxeze", "zxxzy", "zxzee", "zyxzz", "zyyyx", "zzyyy"]
k = 14: xxz=73268415 ["zyy", "xexz", "xxez", "xxze", "zeyy", "zyey", "zyye", "xeexz", "xexez", "exxz", "xexze", "xxeez", "xxeze", "xxxzy", "xxzee", "xyxzz", "xyyyx", "xzyyy", "yxxxy", "yxxyz", "yxxzx", "yxyzz", "yxzxz", "yyzzz", "yzxzz", "yzyyx", "zeeyy", "zeyey", "zeyye", "zxxzz", "zxyyx", "zyeey", "zyeye", "zyyee", "zyzyx", "zzxyx", "zzyxy", "zzyyz", "zzyzx", "zzzxx"]
k = 15: xyx=48513762 ["yxy", "yyz", "yzx", "zxx", "xeyx", "xyex", "xyxe", "yexy", "yeyz", "yezx", "yxey", "yxye", "yyez", "yyze", "yzex", "yzxe", "zexx", "zxex", "zxxe", "xeeyx", "exyx", "xeyex", "xeyxe", "xxzzz", "xyeex", "xyexe", "xyxee", "xyyxz", "xzyxx", "xzzzy", "yeexy", "yeeyz", "yeezx", "yexey", "yexye", "yeyez", "yeyze", "yezex", "yezxe", "yxeey", "yxeye", "yxyee", "yxzyx", "yyeez", "yyeze", "yyxzy", "yyzee", "yzeex", "yzexe", "yzxee", "yzyxz", "zeexx", "zexex", "zexxe", "zxeex", "zxexe", "zxxee", "zxyxz", "zyxxy", "zyxyz", "zyxzx", "zyyzz", "zyzxz", "zzxxz", "zzzyy"]
k = 16: xyy=21563487 ["yzy", "zxy", "zyz", "zzx", "xeyy", "xyey", "xyye", "yezy", "yzey", "yzye", "zexy", "zeyz", "zezx", "zxey", "zxye", "zyez", "zyze", "zzex", "zzxe", "xeeyy", "exyy", "xeyey", "xeyye", "xxxzz", "xxyyx", "xyeey", "xyeye", "xyyee", "xyzyx", "xzxyx", "xzyxy", "xzyyz", "xzyzx", "xzzxx", "yeezy", "yezey", "yezye", "yxxxz", "yxzyy", "yyxxx", "yzeey", "yzeye", "yzyee", "yzzyx", "zeexy", "zeeyz", "zeezx", "zexey", "zexye", "zeyez", "zeyze", "zezex", "zezxe", "zxeey", "zxeye", "zxyee", "zxzyx", "zyeez", "zyeze", "zyxzy", "zyzee", "zzeex", "zzexe", "zzxee", "zzyxz"]
k = 17: xzz=78436512 ["yyx", "xezz", "xzez", "xzze", "yeyx", "yyex", "yyxe", "xeezz", "exzz", "xezez", "xezze", "xxxyy", "xxyzy", "xxzxy", "xxzyz", "xxzzx", "xyyxx", "xyzzy", "xzeez", "xzeze", "xzxzy", "xzzee", "yeeyx", "yeyex", "yeyxe", "yxzzz", "yyeex", "yyexe", "yyxee", "yyyxz", "yzyxx", "yzzzy", "zxyxx", "zxzzy", "zyxyx", "zyyxy", "zyyyz", "zyyzx", "zyzxx", "zzxxx"]
k = 18: yxx=76583214 ["zzy", "yexx", "yxex", "yxxe", "zezy", "zzey", "zzye", "eyxx", "xxyyy", "xyxxz", "xyzyy", "xzxyy", "xzyzy", "xzzxy", "xzzyz", "xzzzx", "yeexx", "yexex", "yexxe", "yxeex", "yxexe", "yxxee", "yxyxz", "yyxxy", "yyxyz", "yyxzx", "yyyzz", "yyzxz", "yzxxz", "yzzyy", "zeezy", "zezey", "zezye", "zxxxz", "zxzyy", "zyxxx", "zzeey", "zzeye", "zzyee", "zzzyx"]
k = 19: yyy=23416785 ["yeyy", "yyey", "yyye", "eyyy", "xxyxx", "xxzzy", "xyxyx", "xyyxy", "xyyyz", "xyyzx", "xyzxx", "xzxxx", "yeeyy", "yeyey", "yeyye", "yxxzz", "yxyyx", "yyeey", "yyeye", "yyyee", "yyzyx", "yzxyx", "yzyxy", "yzyyz", "yzyzx", "yzzxx", "zxxyx", "zxyxy", "zxyyz", "zxyzx", "zxzxx", "zyxyy", "zyyzy", "zyzxy", "zyzyz", "zyzzx", "zzxxy", "zzxyz", "zzxzx", "zzyzz", "zzzxz"]
k = 20: zzz=51486237 ["zezz", "zzez", "zzze", "ezzz", "xxxyx", "xxyxy", "xxyyz", "xxyzx", "xxzxx", "xyxyy", "xyyzy", "xyzxy", "xyzyz", "xyzzx", "xzxxy", "xzxyz", "xzxzx", "xzyzz", "xzzxz", "yxyyy", "yyxxz", "yyzyy", "yzxyy", "yzyzy", "yzzxy", "yzzyz", "yzzzx", "zeezz", "zezez", "zezze", "zxxyy", "zxyzy", "zxzxy", "zxzyz", "zxzzx", "zyyxx", "zyzzy", "zzeez", "zzeze", "zzxzy", "zzzee"]
k = 21: xxxy=84375126 ["xxyz", "xxzx", "xyzz", "xzxz", "yzzz", "zxzz", "zyyx", "xexxy", "xexyz", "xexzx", "xeyzz", "xezxz", "xxexy", "xxeyz", "xxezx", "xxxey", "xxxye", "xxyez", "xxyze", "xxzex", "xxzxe", "xyezz", "xyzez", "xyzze", "xzexz", "xzxez", "xzxze", "yezzz", "yzezz", "yzzez", "yzzze", "zexzz", "zeyyx", "zxezz", "zxzez", "zxzze", "zyeyx", "zyyex", "zyyxe"]
k = 22: xxyx=14852376 ["xyxy", "xyyz", "xyzx", "xzxx", "yxyy", "yyzy", "yzxy", "yzyz", "yzzx", "zxxy", "zxyz", "zxzx", "zyzz", "zzxz", "xexyx", "xeyxy", "xeyyz", "xeyzx", "xezxx", "xxeyx", "xxyex", "xxyxe", "xyexy", "xyeyz", "xyezx", "xyxey", "xyxye", "xyyez", "xyyze", "xyzex", "xyzxe", "xzexx", "xzxex", "xzxxe", "yexyy", "yeyzy", "yezxy", "yezyz", "yezzx", "yxeyy", "yxyey", "yxyye", "yyezy", "yyzey", "yyzye", "yzexy", "yzeyz", "yzezx", "yzxey", "yzxye", "yzyez", "yzyze", "yzzex", "yzzxe", "zexxy", "zexyz", "zexzx", "zeyzz", "zezxz", "zxexy", "zxeyz", "zxezx", "zxxey", "zxxye", "zxyez", "zxyze", "zxzex", "zxzxe", "zyezz", "zyzez", "zyzze", "zzexz", "zzxez", "zzxze"]
k = 23: xyxx=37842651 ["xzzy", "yxyx", "yyxy", "yyyz", "yyzx", "yzxx", "zxxx", "xeyxx", "xezzy", "xyexx", "xyxex", "xyxxe", "xzezy", "xzzey", "xzzye", "yexyx", "yeyxy", "yeyyz", "yeyzx", "yezxx", "yxeyx", "yxyex", "yxyxe", "yyexy", "yyeyz", "yyezx", "yyxey", "yyxye", "yyyez", "yyyze", "yyzex", "yyzxe", "yzexx", "yzxex", "yzxxe", "zexxx", "zxexx", "zxxex", "zxxxe"]
k = 24: xyyy=62157348 ["yxxz", "yzyy", "zxyy", "zyzy", "zzxy", "zzyz", "zzzx", "xeyyy", "xyeyy", "xyyey", "xyyye", "yexxz", "yezyy", "yxexz", "yxxez", "yxxze", "yzeyy", "yzyey", "yzyye", "zexyy", "zeyzy", "zezxy", "zezyz", "zezzx", "zxeyy", "zxyey", "zxyye", "zyezy", "zyzey", "zyzye", "zzexy", "zzeyz", "zzezx", "zzxey", "zzxye", "zzyez", "zzyze", "zzzex", "zzzxe"]
]


Note that $xy$ means the composition $x \circ y$, thus first applying $y$ and then applying $x$.

Here is the chat log and here is what I told Ruby about the problem. The latter one includes fancy ASCII graphics which explain how the permutations were defined.

The key ideas are

• to describe the rotations as permutations on the eight labeled vertices of a cube
• every rotation is a composition of the four basic rotations $e, x, y, z$
• we generate compositions by using base $4$ numbers, which got the digits $0, 1, 2, 3$ and then counting from $0$ to $4^n-1$ which gives sequences from length $1$ to length $n$ (due to the normalization of leading zero digit and the rules of addition not all combinations with possible $e$ rotations show up, but all the combinations for $x,y,z$ should occur)
• we keep a hash dictionary which uses the permutation string as key and a permutation object plus a list of known alias name strings as value to sort into the expected $6\cdot 4 = 24$ equivalence classes

It should be possible to just use $x,y,z$ rotations, but then one needs a different mechanism to generate the tree of possible compositions. Counting seemed a quick hack.

For example, if you rotate a cube 90 degrees around the x axis and then 180 degrees around the y axis, it's the same as rotating it 270 degrees around the x axis and then 180 degrees around the z axis.

These would be the compositions $yyx$ and $zzxxx$, which fall into the equivalence class $k = 17$.

References:

• Thanks for the answer, but to clarify, I am only able to rotate this cube around the x, y, or z axis, and that means only by multiples of 90 degrees. Sep 3, 2015 at 12:44
• Thanks, I understand your answer, but I'm looking for a list of all those rotations without duplicates because there are some duplicates that are not immediately obvious. For example, if you rotate a cube 90 degrees around the x axis and then 180 degrees around the y axis, it's the same as rotating it 270 degrees around the x axis and then 180 degrees around the z axis. Sep 3, 2015 at 20:59
• Could you explain that please? Sep 4, 2015 at 15:17
• I added the stuff you needed for you.
– mvw
Sep 4, 2015 at 19:15
• What is a fundamental rotation about $\hat{e}$? Sep 8, 2015 at 13:09