#P9616. Calculating Rocky Dave's Journey on the Sloped Roof

    ID: 22763 Type: Default 1000ms 256MiB

Calculating Rocky Dave's Journey on the Sloped Roof

Calculating Rocky Dave's Journey on the Sloped Roof

On a sunny autumn afternoon, the rock pigeon Rocky Dave sets out to reach his beloved Columba Livia across a complex rooftop. The building's roof is modeled by a simple polygon Z in the x-y plane, and the roof surfaces are sloped at an identical angle. Rocky Dave’s planned route is the straight line (when viewed from above) connecting his starting point to Columba Livia’s position. However, because the roof is inclined, the actual walking distance on the sloped surfaces is longer; it is obtained by dividing the horizontal (projected) length by \( \cos(\theta) \), where \( \theta \) is the slope angle in radians.

When the straight line leaves the building’s boundary, Dave will not walk but will fly in a straight line (at constant altitude) until he reaches the building again. Hence, the journey is segmented into portions that lie inside Z (traversed on the sloped roof) and portions outside Z (traversed by flight). Given the building's plan (as a simple polygon), the roof slope angle \(\theta\) in degrees, and the positions of Rocky Dave and Columba Livia on the roof, your task is to calculate the exact journey length. For the segments on the roof, the travel distance is the horizontal length divided by \(\cos(\theta)\), and for the flying segments, it is the horizontal distance.

Input details:

  • The first line contains an integer \(N\) (\(N \ge 3\)), the number of vertices of the polygon Z.
  • The next \(N\) lines each contain two real numbers representing the coordinates of the polygon's vertices (given in order, forming a simple polygon).
  • The next line contains a real number \(\theta\) (in degrees), representing the roof's slope angle.
  • The following line contains two real numbers representing the x and y coordinates of Rocky Dave's starting position.
  • The last line contains two real numbers representing the x and y coordinates of Columba Livia's position.

Output details:

  • Output a single real number – the exact journey length. The segments that lie inside the polygon are scaled by \(\frac{1}{\cos(\theta)}\), while the ones outside remain unchanged.

Note: Use the ray-casting algorithm to determine if a point lies inside the polygon and standard line-segment intersection methods to determine the intersection points of the straight-line route with the polygon's edges.

inputFormat

The input starts with an integer \(N\), representing the number of vertices of the polygon Z.
Each of the following \(N\) lines contains two space-separated real numbers denoting the x and y coordinates of a vertex of the polygon, given in order.
The next line contains a real number \(\theta\) (in degrees), the roof slope angle.
The following line contains two space-separated real numbers representing Rocky Dave's starting position (x, y).
The last line contains two space-separated real numbers representing Columba Livia's position (x, y).

outputFormat

Print a single real number representing the journey length, computed as follows:
Let \(L\) be the total Euclidean length of the straight-line route from Rocky Dave to Columba Livia. Let \(L_{in}\) be the total length of the parts of the route that lie inside the polygon and \(L_{out}\) be the total length outside.
The final journey length is \(\frac{L_{in}}{\cos(\theta)} + L_{out}\).

sample

4
0 0
10 0
10 10
0 10
45
2 2
8 8
12.000000

</p>