Orig Description
Problem I: Shy Polygons
You are given two solid polygons and their positions on the xy-plane. You can move one
of the two along the x-axis (they can overlap during the move). You cannot move it in other
directions. The goal is to place them as compactly as possible, subject to the following condition:
the distance between any point in one polygon and any point in the other must not be smaller
than a given minimum distance L.
We define the width of a placement as the difference between the maximum and the minimum
x-coordinates of all points in the two polygons.
Your job is to write a program to calculate the minimum width of placements satisfying the
above condition.
Let's see an example. If the polygons in Figure 13 are placed with L = 10.0, the result will be 100. Figure 14 shows one of the optimal placements.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
L
Polygon1
Polygon2
L is a decimal fraction, which means the required distance of two polygons. L is greater than
0.1 and less than 50.0.
The format of each polygon is as follows.
n
x1 y1
x2 y2
.
.
.
xn yn
n is a positive integer, which represents the number of vertices of the polygon. n is greater than
2 and less than 15.
Remaining lines represent the vertices of the polygon. A vertex data line has a pair of nonneg-
ative integers which represent the x- and y-coordinates of a vertex. x- and y-coordinates are
separated by a single space, and y-coordinate is immediately followed by a newline. x and y are
less than 500.
Edges of the polygon connect vertices given in two adjacent vertex data lines, and vertices given
in the last and the first vertex data lines. You may assume that the vertices are given in the
counterclockwise order, and the contours of polygons are simple, i.e. they do not cross nor touch
themselves.
Also, you may assume that the result is not sensitive to errors. In concrete terms, for a given
pair of polygons, the minimum width is a function of the given minimum distance l. Let us
denote the function w(l). Then you can assume that |w(L ± 10-7) - w(L)| < 10-4.
The end of the input is indicated by a line that only contains a zero. It is not a part of a dataset.
Output
The output should consist of a series of lines each containing a single decimal fraction. Each
number should indicate the minimum width for the corresponding dataset. The answer should
not have an error greater than 0.0001. You may output any number of digits after the decimal
point, provided that the above accuracy condition is satisfied.
Sample Input
10.5235
3
0 0
100 100
0 100
4
0 50
20 50
20 80
0 80
10.0
4
120 45
140 35
140 65
120 55
8
0 0
100 0
100 100
0 100
0 55
80 90
80 10
0 45
10.0
3
0 0
1 0
0 1
3
0 100
1 101
0 101
10.0
3
0 0
1 0
0 100
3
0 50
100 50
0 51
0
Output for the Sample Input
114.882476
100
1
110.5005
Extracted Specification
An integer or decimal L (0.1 < L < 50.0), representing a required distance.
Two polygons described as follows:
- Each polygon starts with an integer n (2 < n < 15), representing the number of vertices.
- Followed by n pairs of non-negative integers (x_i and y_i) (0 ≤ x_i, y_i < 500) representing the coordinates of each vertex.
The input ends with a line containing a single zero.
### Example Input:
```
10.5235
3
0 0
100 100
0 100
4
0 50
20 50
20 80
0 80
10.0
4
120 45
140 35
140 65
120 55
8
0 0
100 0
100 100
0 100
0 55
80 90
80 10
0 45
10.0
3
0 0
1 0
0 1
3
0 100
1 101
0 101
10.0
3
0 0
1 0
0 100
3
0 50
100 50
0 51
0
```
### Function Signature:
Write a function f(inputs) that takes in the input.
```python
def f(inputs: List[Tuple[float, List[Tuple[int, int]], List[Tuple[int, int]]]]):
'''
inputs: a list of tuples, each containing:
- a float
- a list of integer pairs representing the first polygon
- a list of integer pairs representing the second polygon
'''
```
Test Case Generator
Sure! Here's a test case generator for the given problem "Shy Polygons":
```python
import random
def generate_polygon():
n = random.randint(3, 14) # Number of vertices (3 <= n < 15)
vertices = []
for _ in range(n):
x = random.randint(0, 499)
y = random.randint(0, 499)
vertices.append((x, y))
return n, vertices
def tcgen():
test_cases = []
while random.random() < 0.8: # Generate multiple test cases
L = round(random.uniform(0.1, 50.0), 4) # Distance L (0.1 < L < 50.0)
n1, polygon1 = generate_polygon()
n2, polygon2 = generate_polygon()
test_case = {
"L": L,
"polygon1": polygon1,
"polygon2": polygon2
}
test_cases.append(test_case)
return test_cases
# Example test case generation
if __name__ == "__main__":
test_cases = tcgen()
for case in test_cases:
print(case["L"])
print(case["polygon1"])
print(case["polygon2"])
```
### Explanation:
1. **generate_polygon()**: This function generates a polygon with a random number of vertices (between 3 and 14 inclusive). Each vertex has x and y coordinates, both of which are between 0 and 499.
2. **tcgen()**: This function generates multiple test cases until a random stopping condition is met (controlled by `random.random() < 0.8`). For each test case:
- A random distance `L` is generated between 0.1 and 50.0.
- Two random polygons are generated using the `generate_polygon()` function.
- The test case is stored in a dictionary and added to the list of test cases.
3. **Printing Test Cases**: The example at the end of the script shows how to print the generated test cases.
This generator will create diverse and random test cases within the given constraints, suitable for testing the "Shy Polygons" problem.
Extract Arguments
def extract_arguments(fh):
datasets = []
while True:
L = float(fh.readline().strip())
if L == 0:
break
n1 = int(fh.readline().strip())
polygon1 = [tuple(map(int, fh.readline().strip().split())) for _ in range(n1)]
n2 = int(fh.readline().strip())
polygon2 = [tuple(map(int, fh.readline().strip().split())) for _ in range(n2)]
datasets.append((L, polygon1, polygon2))
return datasets
# Example usage:
# with open('input.txt', 'r') as fh:
# datasets = extract_arguments(fh)
# for dataset in datasets:
# print(dataset)