We simply need to compute which of the five events that could stop the motion will occur first. Recall that the events are:
We can note right away that it isn't necessary to check for the second event directly, since the last event, that j0, j1 and jp-1 become colinear, will occur before (or at the same time as) j1 straightens.
The key to solving this problem is to notice that the distance m from j1 to jp always increases during a move. This is almost obvious: j1 rotates counterclockwise about j0 while both j0 and jp remain at the same position. Also, we showed in the proof of correctness that j1 always remains in the wedge defined by u and v. Then the angle jpj0j1 always increases, thereby increasing the distance m.
We can use the law of cosines to solve for m in the case of each event, then the stopping event is simply that for which m is the smallest.
Solving the four equations so obtained always takes the same amount of time, so each move takes a constant amount of time. Now we need to see how many moves may be required.
So we just need to determine how many moves it can take to straighten one of the joints.
We only stop a move if a joint has straightened or if j0, j1 and jp-1 become colinear. We've already determined that the first can only occur n times. Now we'll show that the second can only happen up to n-3 times for each straightened reflex joint.
Whenever the three points become colinear, we decrement p and execute another move. In the beginning, p is at most n-1. And we know that p is always at least 3. So we can only decrement it n-3 times for each reflex vertex.
Finally, at most O( n2 ) moves are needed, each of which takes a constant amount of time to compute, so the running time of the algorithm is quadratic.