#P3738. Vertical Path Length in a Polygon

    ID: 16989 Type: Default 1000ms 256MiB

Vertical Path Length in a Polygon

Vertical Path Length in a Polygon

A hostile area is represented by a closed polygon with vertices (P_1(x_1,y_1)), (P_2(x_2,y_2)), (\cdots), (P_n(x_n,y_n)). Our scout ZDM-007 is planning to cross the enemy blockade following a path that is vertical (i.e. perpendicular to the (X)-axis) from south to north. Given the current position of ZDM-007, which fixes his (x)-coordinate to (k), compute the total length of his route that lies within the enemy territory.

More formally, you are given a polygon and a vertical line (x=k). Your task is to compute the total length of the intersection of this vertical line with the polygon. Note that the polygon is simple and its vertices are given in order.

The intersection between a vertical line and a polygon typically consists of several line segments. To solve the problem, determine the intersection points between the vertical line and every edge of the polygon (using the half-open interval technique: for an edge between (P_i) and (P_j), if ((x_i<k\ and\ x_j\ge k)) or ((x_i\ge k\ and\ x_j<k)), then compute the intersection). After sorting these intersection points by their (y)-coordinates, pair them consecutively (the first with the second, the third with the fourth, etc.) and sum the lengths of these segments.

inputFormat

The first line contains two numbers: an integer (n) (the number of vertices of the polygon) and a float (k) (the fixed (x)-coordinate of ZDM-007). Each of the next (n) lines contains two floating-point numbers representing the coordinates (x_i) and (y_i) of the vertex (P_i). The vertices are given in order (either clockwise or counterclockwise).

outputFormat

Output a single floating-point number which is the total length of the part of the vertical line (x=k) that lies inside the polygon. The answer should be printed with at least six decimal places.

sample

4 5
0 0
10 0
10 10
0 10
10.000000