Despite the long title, my question is fairly simple. Let’s say you have a cube in 3D-space and you want to rotate it along the three axes based on the set of instructions provided by a user or elsewhere in the program. How would you simplify, if you will, that set of rotations?
For simplicity’s sake, we’ll use the notation involved in rotating a Rubik’s cube. (And also because that’s what I’m programming.. But that’s unimportant.) So, X would be a clockwise rotation along the X axis, X’ would be a counterclockwise rotation along the X axis, and X2 would be two clockwise rotations rotations along the X axis.
Now, let’s say I have a set of instructions X Y X'
. That set of instructions did the same thing that the single instruction of Z
would have done. Or, in a simpler case, if I had X X'
or X2 X2
, those rotations cancel out.
My question is, what is the best way – from a programming perspective – to simplify these sets of instructions?
Instead of representing a rotation with “# degrees around the ? axis” you should use a Rotation Matrix.
Rotation matrices have a nice property that if you multiple them you’d get a rotation matrix that has the same effect as applying all the rotations one after another(order is important here!)
Another nice property that you might need later is that if you apply a rotation matrix to a Translation Vector, apply the rotation matrix to an object and then the rotated translation vector to the result, it’ll have the same effect as applying the original translation vector to the object and then applying the rotation matrix. This means that if later you’d want to add translations to that set of yours you could easily convert such a set to a single rotation and a single translation.
From personal experience using a set to keep track of each rotational part of the cube works well. Each sub cube is in three sets no mater the size of the rubik cube. So to find a sub cube some where on the rubik’s cube you just take the intersection of the three sets (the result is one sub cube). To do a move remove the effected sub cubs from the sets involved in the move and then put them back into the sets that take them as a result of the move.
The 4 by 4 cube will have 12 sets. 6 sets for the 6 faces and 6 sets for the six bands that go around the cube. The faces each have 16 sub cubes and the bands each have 12 sub cubs. There are a total of 56 sub cubes. Each sub cube holds information about color and the direction of the colors. The rubik cube itself is a 4 by 4 by 4 array with each element having information consisting of the 3 sets that define the sub cube at that location.
This data structure has you using the intersection of sets to define each sub blocks location in the cube. This save the work of having to update the near sub blocks when a change is made.