1

I have a model of a 3D object in my program. I can rotate it by 90 degrees along a axis of my coordinate system. I can choose the axis for every rotation. I can apply several rotations after another.

For example I could do:

object = create_3d_obj();
rotated_ob = rotate_90_degrees(object, X_axis);
even_more_rob = rotate_90_degrees(rotated_ob, X_axis);
very_heavily_rob = rotate_90_degrees(even_more_rob, Y_axis);

However, (as expected) rotating 4 times by 90 degree along the same axis results in the original object. That leaves me with a finite set of rotated objects I can reach with these 90 degree rotations.

How many rotated objects are there? How can I iterate over that set of rotated objects?

RotatingPieces
  • 413
  • 2
  • 8

2 Answers2

0

The problem with this is that the order of rotations matters and that you can reach the same state with different ways, which is why nested loops don't work (they give you 64 results, while you only have 24). For that reason, you need to eliminate existing orientations from the evaluation, otherwise you have duplicates. Comparing orientations is therefore the first job, you need a definitions that represents the orientation, used result and input of the rotation operations.

Then, a simple approach in Python to finding rotations is this one:

result = {}
new = {o0} # starting orientation
while new:
    cur = new.pop()
    # add new orientation to results
    result.add(cur)
    # determine derived orientations from the current one
    # that weren't considered before
    for o in derived_orientations(cur):
        if not o in result:
            new.add(o)

Note that you could also use the nested loops and filter the duplicates from the results, but this approach basically proves its own correctness (and it will loop infinitely if there were an infinite number of orientations), while with the nested loops that isn't obvious to me.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
-1

As mentioned, three for loop can do the job.

Unlike a regular for loop, you need to reset the state of rotation of each axis when you exit it.

for ( int drx = 0 ; drx < 4 ; ++ drx )
{
    rotate_90_degrees(object, X_axis);
    for ( int dry = 0 ; dry < 4 ; ++ dry )
    {
        rotate_90_degrees(object, Y_axis);
        for ( int drz = 0 ; drz < 4 ; ++ drz )
        {
            rotate_90_degrees(object, Z_axis);
            //do something with this iteration!
        }
        rotate_90_degrees(object, Z_axis);
    }
    rotate_90_degrees(object, Y_axis);
}
rotate_90_degrees(object, X_axis); //if you want the state to be restored to original, add this.

Now this only do iteration as you asked, its first iteration starts at (90, 90, 90). Sure you can do some modification to fix this if needed.

Shane Hsu
  • 7,937
  • 6
  • 39
  • 63