-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path10005-Packing polygons.cpp
59 lines (53 loc) · 1.73 KB
/
10005-Packing polygons.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
#include <bits/stdc++.h>
using namespace std;
#define EPS 1e8
struct point {
double x, y;
point() { x = y = 0.0; }
point(double _x, double _y) : x(_x), y(_y) {}
bool operator < (point other) const { // override less than operator
if (fabs(x - other.x) > EPS) // useful for sorting
return x < other.x; // first criteria , by x-coordinate
return y < other.y;
}
};
double dist(point p1, point p2) { // Euclidean distance
// hypot(dx, dy) returns sqrt(dx * dx + dy * dy)
return hypot(p1.x - p2.x, p1.y - p2.y);
}
bool circle2PtsRad(point p1, point p2, double r, point &c) {
double d2 = (p1.x - p2.x) * (p1.x - p2.x) +
(p1.y - p2.y) * (p1.y - p2.y);
double det = r * r / d2 - 0.25;
if (det < 0.0) return false;
double h = sqrt(det);
c.x = (p1.x + p2.x) * 0.5 + (p1.y - p2.y) * h;
c.y = (p1.y + p2.y) * 0.5 + (p2.x - p1.x) * h;
return true;
}
int main() {
int n;
double rad;
point c;
while(scanf("%d",&n), n){
vector<point> polygon;
for(int i=0;i<n;i++) {
point p;
scanf("%lf %lf",&p.x,&p.y);
polygon.push_back(p);
}
scanf("%lf",&rad);
bool foundCircle = false;
for(int i=0;i<n && !foundCircle;i++){
for(int j=0;j<n && !foundCircle;j++){
if(i==j) continue;
circle2PtsRad(polygon[i],polygon[j],rad,c);
foundCircle = true;
for(int k=0;k<n && foundCircle;k++)
if(dist(polygon[k],c) > rad) foundCircle = false;
}
}
if(foundCircle) printf("The polygon can be packed in the circle.\n");
else printf("There is no way of packing that polygon.\n");
}
}