Under construction

Still being built. Things may change

Cutting a Polygon

Time limit1sMemory limit128 MB

Summary
For each of several cutting lines, compute the total length of the intersection of the line with a given simple polygon.
Level

Medium7 of 10

Topics
Geometry, Implementation, Sorting, Brute force
Solved
No attempts yet

Problem

A simple polygon (not necessarily convex) and a line in the plane are given. If the polygon is cut along the line, it may split into several smaller polygons. Find the length of the cut, that is, the total length of the segments that form the intersection of the line and the polygon.

Input

The input consists of several test cases. Each test case begins with a line containing two non-negative integers nn and mm (3≤n≤10003 \le n \le 1000): the number of vertices of the polygon and the number of cutting lines to consider. The next nn lines contain the xx and yy coordinates of the vertices, given in either clockwise or counterclockwise order. Each of the following mm lines contains four numbers x1 y1 x2 y2x_1\ y_1\ x_2\ y_2, the coordinates of two distinct points that define a cutting line. If a vertex of the polygon is closer than 10e-8 to a cutting line, the vertex is considered to lie on the line. The input is terminated by a line with nn and mm both equal to 00, which is not processed.

Output

For each cutting line, print on its own line the total length of the segments in the intersection of the line and the polygon, rounded to exactly 3 digits after the decimal point. The perimeter of the polygon is considered to belong to the polygon.

Hint

Because the perimeter of the polygon belongs to the polygon, an edge that lies exactly along the cutting line contributes its full length to the intersection, whereas a line that meets the polygon at only a single vertex contributes a length of 00.

Examples1

  1. Example 1

    Input
    9 5
    0 0
    0 2
    1 1
    2 2
    3 1
    4 2
    5 1
    6 2
    6 0
    -1 2 7.5 1
    0 1 6 1
    0 1.5 6 1.5
    0 2 6 1
    0 0 0 2
    0 0
    
    Expected output
    2.798
    6.000
    3.000
    2.954
    2.000