This was an interesting problem. The problem gives a series of decisions taken at some particular segments of a fixed length wire. It asks for the orientation of the last segment, i.e., the segment between $(L - 1, 0, 0)$ and $(L, 0, 0)$.
We can solve this problem with the knowledge of cross product. Remember the right hand rule? Given two vectors $a$ and $b$, their cross product is $a \times b$. The direction of the cross product can be determined from the right hand rule. See the picture below:
For this problem only the direction is important, and not the magnitude. The initial direction of the last segment is the same as the unit vector $(1,0,0)$. We’ll start with this.
The decision are applied at different segments. But we are only interested in the last segment. So, we’ll apply the transformations on the last segments one by one. Either the orientation of the last segment will stay the same, or it will change by $\pm\ddfrac{\pi}{2}$ around an axis.
The axis of rotation will either be parallel to the last segment vector (so the orientation will stay the same), or it will be perpendicular to the last segment vector (so the orientation will change by $\pm\ddfrac{\pi}{2}$).
We have to figure out the axis of rotation in each step. If we know the axis of rotation $\vect{a}$, then we can apply the cross product to know where the last segment will end up.
But how do we find the axis of rotation? The initial vector of each segment can be written as $(1,0,0)$. The decision $+y,-y,+z,-z$ are the result of rotation of $(1,0,0)$ around an axis. Let $\vect{v} = (1,0,0)$ and $\vect{v_{rot}}$ be the any of the rotated vector $+y,-y,+z,-z$. Then the direction of the axis vector can be figured out in the following manner:
\[ \vect{a} = \vect{v_{rot}} \times \vect{v} \]
See this math stackexchange answer as to why the above this true.
Once we’ve found out the axis vector $\vect{a}$, we can just simply apply $\vect{a} \times \vect{v^\prime}$ to find out where the last segment will end up for this iteration. $\vect{v^\prime}$ is the last segment vector at any iteration.
A bit of caution: if the axis vector is in the same direction as $\vect{v^\prime}$, then the cross product will become $\vect{0}$. In this case, the direction of the last segment will not change.
Here’s my implementation in C++
:
Sharing is caring. Share this story in...
Share: Twitter Facebook LinkedIn