#P4227. Infinite Rope Wrapping
Infinite Rope Wrapping
Infinite Rope Wrapping
A playful girl, Jiutiao Kelian, has a wall on which she has hammered n nails with distinct coordinates \((x_i, y_i)\). Then she tacks m ropes onto the wall. For the i-th rope, one end is fixed at point \(s_i=(sx_i,sy_i)\) on the wall and the rope passes through point \(t_i=(tx_i,ty_i)\). The rope has a given length \(L_i\) and is initially taut (so that \(L_i=\|t_i-s_i\|\)).
In the game she plays with each rope the following process occurs. Holding its movable end, she moves it clockwise while keeping the rope taut. In any short time interval the free end exactly follows a circular arc. Initially the free end revolves around the fixed endpoint \(s_i\) (which acts as the "current center"). However, if during its motion the rope touches one of the nails (other than the fixed endpoint), the rope wraps around that nail. At that moment, the nail becomes the new center of rotation, and the effective rope length becomes the length of the remaining segment (which is the tangent segment from the new center to the previous circular arc). The process then continues: the free endpoint now rotates clockwise around the new center with the updated rope length.
You may assume the following simplifying conditions:
- At every moment the distance from the moving end to every nail is at least \(9\times10^{-4}\).
- The nails have pairwise distinct coordinates, although several may be collinear.
- The volumes of the nails and the rope are negligible. The different pieces of the rope do not interfere with each other during the game.
- Initially, the free end is at least \(9\times10^{-4}\) away from any nail.
- The fixed endpoint for each rope does not influence the rope’s motion; that is, the rope will never wrap around its initially fixed endpoint.
Since the rope never stops, after running forever the moving end will be rotating around some nail repeatedly. Jiutiao Kelian is curious: for each rope, if it were to move infinitely, how many times would the center of rotation change (counting the initial center)? It can be shown that this number is always finite.
Geometric interpretation and simulation:
For each rope, let \(O\) be the current center (starting from \(s_i\)) and let \(r\) be the current rope length (initially \(L_i\)). Let the free endpoint be \(E\) and let its direction (angle) relative to \(O\) be \(\theta\) (computed from \(E-O\)). As \(E\) moves clockwise along the circle centered at \(O\) with radius \(r\), consider any nail \(P\) (with \(P\neq O\)) such that \(\|P-O\|\le r\). Let \(\phi\) be the polar angle of the vector \(P-O\) and define the clockwise angular difference from \(\theta\) by
[ \Delta= \begin{cases} \theta - \phi, & \theta \ge \phi, \ \theta+2\pi-\phi, & \theta<\phi. \end{cases} ]
If (\Delta>0) (by at least a tolerance) the rope will eventually hit the nail for the smallest such (\Delta). At that moment, if (d=|P-O|), the new rope length becomes (r' = \sqrt{r^2-d^2}) (the length of the tangent from (P) to the circle of radius (r)). The moving endpoint is located at
[ E = O + r \bigl(\cos(\theta-\Delta),,\sin(\theta-\Delta)\bigr), ]
and after wrapping the new center becomes (P) and the new initial direction for rotation is the polar angle of (E-P). This process repeats until no nail (among the given nails) can be encountered (i.e. there is no candidate nail satisfying (|P-O| \le r) with a positive clockwise angular difference).
Your task is to compute, for each rope, the total number of centers that appear (including the initial center) if the rope were to move forever.
inputFormat
The first line contains two integers \(n\) and \(m\) \( (1 \le n, m \le 1000)\) representing the number of nails and ropes respectively.
The next \(n\) lines each contain two real numbers \(x_i\) and \(y_i\) representing the coordinates of the nails.
Then \(m\) lines follow. Each line contains five real numbers: \(sx_i\), \(sy_i\), \(tx_i\), \(ty_i\) and \(L_i\), where \((sx_i,sy_i)\) is the fixed endpoint of the rope, \((tx_i,ty_i)\) is the initial location of the free endpoint (thus \(L_i = \| (tx_i,ty_i) - (sx_i,sy_i) \|\)), and \(L_i\) is the length of the rope.
outputFormat
For each rope, output a single line containing an integer representing the total number of centers (pivot points) that will come into effect as the rope moves forever. (This count includes the initial fixed endpoint.)
sample
1 1
0.0 0.0
1.0 0.0 0.0 0.0 1.0
1
</p>