forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
10065.cpp
122 lines (113 loc) · 2.44 KB
/
10065.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <bits/stdc++.h>
using namespace std;
#define MAXPOLY 105
struct point
{
int x;
int y;
};
struct polygon
{
int vertexNumber;
point vertex[MAXPOLY];
};
double area(point vertex[], int vertexNumber)
{
double total = 0.0;
for (int i = 0; i < vertexNumber; i++)
{
int j = (i + 1) % vertexNumber;
total += (vertex[i].x * vertex[j].y - vertex[j].x * vertex[i].y);
}
return fabs(total / 2.0);
}
int crossProduct(point first, point second, point third)
{
return (second.x - first.x) * (third.y - first.y) -
(second.y - first.y) * (third.x - first.x);
}
bool left_lower(point first, point second)
{
if (first.x == second.x)
{
return first.y < second.y;
}
else
{
return first.x < second.x;
}
}
void convex_hull(point vertex[], int vertexNumber, polygon &container)
{
if (vertexNumber <= 3)
{
for (int i = 0; i < vertexNumber; i++)
{
container.vertex[i] = vertex[i];
}
container.vertexNumber = vertexNumber;
return;
}
sort(vertex, vertex + vertexNumber, left_lower);
point upper[MAXPOLY], lower[MAXPOLY];
int top;
upper[0] = vertex[0];
upper[1] = vertex[1];
top = 2;
for (int i = 2; i < vertexNumber; i++)
{
upper[top] = vertex[i];
while (top >= 2 && crossProduct(upper[top - 2], upper[top - 1], upper[top]) >= 0)
{
upper[top - 1] = upper[top];
top--;
}
top++;
}
container.vertexNumber = 0;
for (int i = 0; i < top; i++)
{
container.vertex[container.vertexNumber++] = upper[i];
}
lower[0] = vertex[vertexNumber - 1];
lower[1] = vertex[vertexNumber - 2];
top = 2;
for (int i = vertexNumber - 3; i >= 0; i--)
{
lower[top] = vertex[i];
while (top >= 2 && crossProduct(lower[top - 2], lower[top - 1], lower[top]) >= 0)
{
lower[top - 1] = lower[top];
top--;
}
top++;
}
for (int i = 1; i < top - 1; i++)
{
container.vertex[container.vertexNumber++] = lower[i];
}
}
int main(int ac, char *av[])
{
point tile[MAXPOLY];
polygon container;
int vertexNumber, currentCase = 1;
cout.precision(2);
cout.setf(ios::fixed | ios::showpoint);
while (cin >> vertexNumber, vertexNumber)
{
for (int i = 0; i < vertexNumber; i++)
{
cin >> tile[i].x;
cin >> tile[i].y;
}
double used = area(tile, vertexNumber);
convex_hull(tile, vertexNumber, container);
cout << "Tile #" << currentCase++ << endl;
double all = area(container.vertex, container.vertexNumber);
double rate = (1.0 - used / all) * 100.0;
cout << "Wasted Space = " << rate << " %" << endl;
cout << endl;
}
return 0;
}