-
Notifications
You must be signed in to change notification settings - Fork 2
/
halfPlanes.cpp
258 lines (230 loc) · 5.49 KB
/
halfPlanes.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <map>
#include <complex>
using namespace std;
#define double long double
#define pb push_back
#define ppb pop_back
const double eps = 1e-12;
const double inf = 1e10;
const double pi = acosl((double)-1);
struct point{
double x, y;
int num;
point(){}
point(double xx, double yy, int nnum){
x = xx;
y = yy;
num = nnum;
}
};
struct line{
double a, b, c, ang;
int num;
};
const point vertex[4] = {point(-inf, -inf, 0), point(inf, -inf, 0), point(inf, inf, 0), point(-inf, inf, 0)};
inline bool qq(double a, double b){
return fabs(a - b) < eps;
}
inline bool lq(double a, double b){
return b - a > -eps;
}
inline bool ls(double a, double b){
return b - a >= eps;
}
inline double sqr(double a){
return a * a;
}
inline double len(const point &a){
return sqr(a.x) + sqr(a.y);
}
inline double vec(const point &a, const point &b){
return a.x * b.y - b.x * a.y;
}
inline double dist(line &l, point &p){
return l.a * p.x + l.b * p.y + l.c;
}
point operator + (const point &a, const point &b){
return point(a.x + b.x, a.y + b.y, a.num);
}
point operator - (const point &a, const point &b){
return point(a.x - b.x, a.y - b.y, a.num);
}
point operator / (const point &a, int size){
return point(a.x / size, a.y / size, a.num);
}
bool operator < (const point &a, const point &b){
double v = vec(a, b);
if(qq(v, 0)){
return len(a) < len(b);
}
return v > 0;
}
bool operator == (const line &a, const line &b){
return qq(a.ang, b.ang);
}
bool operator < (const line &a, const line &b){
if(!qq(a.ang, b.ang)){
return a.ang < b.ang;
}
return a.c < b.c;
}
inline bool better(point &a, point &b){
if(!qq(a.x, b.x)){
return a.x < b.x;
}
return a.y < b.y;
}
inline point intersect(line &l1, line &l2){
double d = l1.a * l2.b - l2.a * l1.b;
return point((l1.b * l2.c - l2.b * l1.c) / d, (l2.a * l1.c - l1.a * l2.c) / d, 0);
}
inline line getline(const point &a, const point &b){
line result;
result.a = a.y - b.y;
result.b = b.x - a.x;
result.c = vec(a, b);
return result;
}
inline bool bad(point &a, point &b, point &c){
point v1 = b - a, v2 = c - a;
double v = vec(v1, v2);
if(qq(v, 0)){
return len(v1) < len(v2);
}
return v < 0;
}
vector <point> getconvex(vector <point> points){
for(int i = 1; i < (int)points.size(); i++){
if(better(points[i], points[0])){
swap(points[0], points[i]);
}
}
for(int i = 1; i < (int)points.size(); i++){
points[i] = points[i] - points[0];
}
sort(points.begin() + 1, points.end());
for(int i = 1; i < (int)points.size(); i++){
points[i] = points[i] + points[0];
}
vector <point> convex;
for(int i = 0; i < (int)points.size(); i++){
while(convex.size() >= 2 && bad(convex[convex.size() - 2], convex[convex.size() - 1], points[i])){
convex.ppb();
}
convex.pb(points[i]);
}
return convex;
}
vector <line> getintersection(vector <line> lines){
for(int i = 0; i < (int)lines.size(); i++){
lines[i].ang = atan2l(lines[i].b, lines[i].a);
}
sort(lines.begin(), lines.end());
lines.resize(unique(lines.begin(), lines.end()) - lines.begin());
int size = lines.size();
for(int i = 0; i < size; i++){
lines[i].num = i;
}
for(int i = 0; i < size; i++){
line curline = lines[i];
curline.ang += 2 * pi;
lines.pb(curline);
}
vector <line> intersection;
for(int i = 0; i < (int)lines.size(); i++){
while(intersection.size() >= 2){
if(lq(pi, lines[i].ang - intersection[intersection.size() - 2].ang)){
break;
}
point curpoint = intersect(intersection[intersection.size() - 2], lines[i]);
if(!lq(0, dist(intersection[intersection.size() - 1], curpoint))){
break;
}
intersection.ppb();
}
intersection.pb(lines[i]);
}
vector <int> last(size, -1);
vector <line> result;
for(int i = 0; i < (int)intersection.size(); i++){
if(last[intersection[i].num] == -1){
last[intersection[i].num] = i;
continue;
}
for(int j = last[intersection[i].num]; j < i; j++){
result.pb(intersection[j]);
}
break;
}
return result;
}
inline point readpoint(int num){
point result;
scanf("%Lf %Lf", &result.x, &result.y);
result.num = num;
return result;
}
inline void fail(){
printf("Impossible\n");
exit(0);
}
int main(){
//freopen("tests.in", "r", stdin);
//freopen("tests.out", "w", stdout);
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int n;
scanf("%d", &n);
vector <point> points;
for(int i = 0; i < n; i++){
points.pb(readpoint(i));
}
vector <line> lines;
for(int i = 0; i < n - 1; i++){
lines.pb(getline(points[i], points[i + 1]));
}
vector <point> convex = getconvex(points);
for(int i = 0; i < (int)convex.size(); i++){
point a = convex[i], b = convex[(i + 1) % convex.size()];
if(a.num > b.num){
swap(a, b);
}
lines.pb(getline(a, b));
}
for(int i = 0; i < 4; i++){
lines.pb(getline(vertex[i], vertex[(i + 1) % 4]));
}
vector <line> intersection = getintersection(lines);
if(intersection.size() == 0){
fail();
}
point ans(0, 0, 0);
for(int i = 0; i < (int)intersection.size(); i++){
point curpoint = intersect(intersection[i], intersection[(i + 1) % intersection.size()]);
ans = ans + curpoint;
}
ans = ans / intersection.size();
bool good = true;
for(int i = 0; i < n - 1; i++){
line curline = getline(points[i], points[i + 1]);
if(!ls(0, dist(curline, ans))){
good = false;
break;
}
}
if(!good){
fail();
}
printf("Possible\n%.18Lf %.18Lf\n", ans.x, ans.y);
return 0;
}