Twirl Around

Time limit1sMemory limit128 MB

Summary
A bar inside a simple polygon rotates clockwise, pivoting on the wall whenever a new contact point appears; report the final position of one end, or the position when it jams.
Level

Hard9 of 10

Topics
Geometry, Simulation, Implementation, Math
Solved
No attempts yet

Problem

A room on a flat plane is enclosed by a polygonal wall. Inside it a bar of length LL turns clockwise, like a twirling baton.

At the start one end of the bar (end A) sits at (0,0)(0, 0) and the other end (end B) sits at (0,L)(0, L). At that moment the bar touches the wall only at end A.

The bar turns around a single point where it touches the wall. As soon as another part of the bar reaches the wall, that new touching point becomes the center of rotation.

Find the coordinates of end A once the bar has turned 2πR2\pi R radians clockwise in total.

The bar can also get stuck along the way. That is the state where no touching point lets it turn clockwise any further. The motion ends there, and the answer is the position of end A at that moment.

You may assume that when the length LL changes by ε\varepsilon with ∣ε∣<0.00001|\varepsilon| < 0.00001, the final coordinates (x,y)(x, y) change by no more than 0.00050.0005.

Input

The input holds several datasets. There are at most 100 of them. The end of the input is marked by 0 0 0.

Each dataset has the following format.

L R N
X1 Y1
X2 Y2
...
XN YN

LL is the length of the bar. The bar turns 2πR2\pi R radians unless it gets stuck first. NN is the number of vertices of the polygonal wall.

The vertices of the polygon are given in counter-clockwise order. The polygon is simple, so its border never crosses or touches itself.

NN, XiX_i and YiY_i are integers, and LL and RR are decimal fractions. The ranges are as follows.

  • 1.0≤L≤500.01.0 \le L \le 500.0
  • 1.0≤R≤10.01.0 \le R \le 10.0
  • 3≤N≤1003 \le N \le 100
  • −1000≤Xi≤1000-1000 \le X_i \le 1000
  • −1000≤Yi≤1000-1000 \le Y_i \le 1000
  • X1≤−1X_1 \le -1, Y1=0Y_1 = 0
  • X2≥1X_2 \ge 1, Y2=0Y_2 = 0

Output

For each dataset, print the final position of end A on one line. The judge compares the output exactly, so write the coordinates as integers: multiply xx and yy by 1000, round each to the nearest integer, and print the two integers separated by one space.

In the test data, 1000x1000x and 1000y1000y stay at least 10−410^{-4} away from a rounding boundary, that is from any integer plus 0.50.5, so the rounded value is uniquely determined.

Examples1

  1. Example 1

    Input
    4.0 2.0 8
    -1 0
    5 0
    5 -2
    7 -2
    7 0
    18 0
    18 6
    -1 6
    4.0 2.0 4
    -1 0
    10 0
    10 12
    -1 12
    4.0 1.0 7
    -1 0
    2 0
    -1 -3
    -1 -8
    6 -8
    6 6
    -1 6
    4.0 2.0 6
    -1 0
    10 0
    10 3
    7 3
    7 5
    -1 5
    5.0 2.0 6
    -1 0
    2 0
    2 -4
    6 -4
    6 6
    -1 6
    6.0 1.0 8
    -1 0
    8 0
    7 2
    9 2
    8 4
    11 4
    11 12
    -1 12
    0 0 0
    
    Expected output
    16000 0
    10000 7464
    586 -5414
    8000 0
    6000 0
    9528 4000