Cutting a Polygon
Time limit1sMemory limit128 MB
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 and (): the number of vertices of the polygon and the number of cutting lines to consider. The next lines contain the and coordinates of the vertices, given in either clockwise or counterclockwise order. Each of the following lines contains four numbers , 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 and both equal to , 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 .