eolymp
bolt
Try our new interface for solving problems
Problems

Intersection of nonconvex polygons

Intersection of nonconvex polygons

Most drawing or illustration programs have simple tools for creating polygon objects. The better ones can find the regions that are the intersections of two polygons. The picture below shows two polygons, one is a pentagon and the other is a triangle. Their intersection consists of the two dark regions. \includegraphics{https://static.e-olymp.com/content/95/957a6352cf634c39328f0fa7e823db588154df07.jpg} IBM has just hired you as a member of a programming team that will create a very sophisticated drawing/illustration program. Your task is to write the part of the program that deals with polygon intersections. Your boss has told you to delay work on the user interface and focus only on the geometric representations of the intersections. A polygon in the Cartesian plane can be represented by a sequence of points that are its vertices. The vertices in the sequence appear in the order in which they are visited when traveling clockwise around the polygon's boundary; so any two adjacent vertices in the sequence are the endpoints of a line segment that is one of the polygon's sides. The last and the first vertices in the sequence are also endpoints of a side. Vertices are identified by their \textbf{x} and \textbf{y} coordinates. Assume the following about each polygon: \begin{itemize} \item No point will occur as a vertex (on the same polygon) more than once. \item Two sides can intersect only at a common endpoint (vertex). \item The angle between any two sides with a common vertex has a measure that is greater than \textbf{0} and less than\textbf{ 360}. \item The polygon has at least \textbf{3} vertices. \end{itemize} The intersection of two polygons consists of \textbf{0} or more connected regions. Your problem is to take two polygons and determine the regions of their intersection that are polygons satisfying the criteria above. \InputFile The input contains several data sets, each consisting of two polygons. Each polygon appears as a sequence of numbers: \textbf{n}, \textbf{x_1}, \textbf{y_1}, \textbf{x_2}, \textbf{y_2}, ..., \textbf{x_n}, \textbf{y_n} where the integer \textbf{n} is the number of vertices of the polygon, and the real coordinates (\textbf{x_1}, \textbf{y_1}) through (\textbf{x_n}, \textbf{y_n}) are the boundary vertices. The end of input is indicated by two zeroes for the values of \textbf{n}. These two zeroes merely mark the end of data and should not be treated as an additional data set. \OutputFile For each data set, your program should output its number (\textbf{Data set 1}, \textbf{Data set 2}, etc.), and the number of regions in the intersection of its two polygons. Label each region in the data set (\textbf{Region 1}, \textbf{Region 2}, etc.) and list its vertices in the order they appear when they are visited going either clockwise or counterclockwise around the boundary of the region. The first vertex printed should be the vertex with the smallest \textbf{x}-coordinate (to break ties, use the smallest \textbf{y}-coordinate). No region may include degenerate parts (consisting of adjacent sides whose angle of intersection is \textbf{0}). If the three endpoints of two adjacent sides are collinear, the two sides should be merged into a single side. Print each vertex in the standard form (\textbf{x}, \textbf{y}), where \textbf{x} and \textbf{y} have two digits to the right of the decimal. The following sample input contains exactly one data set. (The data set corresponds to the illustration at the beginning of this problem description.)
Time limit 2 seconds
Memory limit 256 MiB
Input example #1
3 2 1 0.5 3.5 8 5
5 1.5 3 2 7 6.5 6.5 6.5 3.25 4 4.5

0
0
Output example #1
Data Set 1
Number of intersection regions: 2
Region 1:(1.500000000000000,3.000000000000000)(1.589743589743590,3.717948717948718)(3.250000000000000,4.050000000000000)
Region 2:(4.428571428571429,4.285714285714286)(6.500000000000000,4.700000000000000)(6.500000000000000,4.000000000000000)(5.857142857142857,3.571428571428572)