-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfence4.cpp
202 lines (186 loc) · 4.58 KB
/
fence4.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
/*
TASK:fence4
LANG:C++
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
struct Vector
{
double x, y;
Vector(double x=0, double y=0) : x(x), y(y)
{
}
Vector operator + (Vector rbs)
{
return Vector(x + rbs.x, y + rbs.y);
}
Vector operator - (Vector rbs)
{
return Vector(x - rbs.x, y - rbs.y);
}
Vector operator * (double vari)
{
return Vector(x * vari, y * vari);
}
}v[205], per;
const double eps = 1e-10;
int dcmp(double x)
{
if (fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
double cross(Vector A, Vector B)
{
return A.x * B.y - A.y * B.x;
}
bool judge(Vector a1, Vector a2, Vector b1, Vector b2)
{
double c1 = cross(a2 - a1, b1 - a1), c2 = cross(a2 - a1, b2 - a1);
double c3 = cross(b2 - b1, a1 - b1), c4 = cross(b2 - b1, a2 - b1);
return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
}
bool judge2(Vector a1, Vector a2, Vector b1, Vector b2)
{
double c1 = cross(a2 - a1, b1 - a1), c2 = cross(a2 - a1, b2 - a1);
return dcmp(c1) * dcmp(c2) < 0;
}
Vector getv(Vector P, Vector v, Vector Q, Vector w)
{
Vector u = P - Q;
double t = cross(w, u) / cross(v, w);
return P + (v * t);
}
double sqr(double x)
{
return x * x;
}
double dist(Vector a, Vector b)
{
return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));
}
int n, ansn, ans[205][2];
double mindis[205];
bool coverv[205];
int main()
{
freopen("fence4.in", "r", stdin);
freopen("fence4.out", "w", stdout);
scanf("%d", &n);
scanf("%lf%lf", &per.x, &per.y);
for (int i = 0; i < n; ++i) scanf("%lf%lf", &v[i].x, &v[i].y);
bool flag = true;
for (int i = 0; i < n; ++i)
{
for (int j = i + 2; j < n; ++j)
if (judge(v[i], v[(i+1) % n], v[j], v[(j+1) % n]))
{
flag = false;
break;
}
if (!flag) break;
}
if (!flag) printf("NOFENCE\n");
else
{
for (int i = 0; i < n; ++i) mindis[i] = 1e20;
memset(coverv, false, sizeof(coverv));
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (j != i && j+1 != i)
{
if (judge2(per, v[i], v[j], v[(j+1) % n]))
{
Vector tmpv = getv(per, per-v[i], v[j], v[j]-v[(j+1) % n]);
if (dcmp(tmpv.x-per.x) * dcmp(v[i].x-per.x) < 0 || dcmp(tmpv.y-per.y) * dcmp(v[i].y-per.y) < 0) continue;
mindis[i] = min(mindis[i], dist(per, tmpv));
}
}
if (i != j && cross(v[i]-per, v[j]-per) == 0)
{
if (dcmp(v[j].x-per.x) * dcmp(v[i].x-per.x) < 0 || dcmp(v[j].y-per.y) * dcmp(v[i].y-per.y) < 0) continue;
if (dist(v[i], per) < dist(v[j], per)) coverv[j] = true;
else coverv[i] = true;
}
}
}
ansn = 0;
for (int i = 0; i < n; ++i)
{
if (cross(v[i] - per, v[(i+1) % n] - per) == 0) continue;
double d1 = dist(per, v[i]), d2 = dist(per, v[(i+1) % n]);
int j = i;
bool canbeseen = true;
if (d1 > mindis[j] || coverv[j])
{
for (;;)
{
if (j == -1) j = n - 1;
if (j == (i+1) % n)
{
canbeseen = false;
break;
}
if (!coverv[j])
{
int left = (j-1+n)%n, right = (j+1)%n;
if (dcmp(cross(v[j]-per, v[left]-per)) * dcmp(cross(v[j]-per, v[right]-per)) > 0)
if (judge2(per, v[j], v[i], v[(i+1) % n]))
{
Vector tmpv = getv(per, per-v[j], v[i], v[i]-v[(i+1) % n]);
if (abs(mindis[j] - dist(per, tmpv)) < eps) break;
}
}
j--;
}
}
int tmp = j;
j = (i+1) % n;
if (d2 > mindis[j] || coverv[j])
{
for (;;)
{
if (j == n) j = 0;
if (j == i)
{
canbeseen = false;
break;
}
if (!coverv[j])
{
int left = (j-1+n)%n, right = (j+1)%n;
if (dcmp(cross(v[j]-per, v[left]-per)) * dcmp(cross(v[j]-per, v[right]-per)) > 0)
if (judge2(per, v[j], v[i], v[(i+1) % n]))
{
Vector tmpv = getv(per, per-v[j], v[i], v[i]-v[(i+1) % n]);
if (abs(mindis[j] - dist(per, tmpv)) < eps) break;
}
}
j++;
}
}
if (canbeseen && (cross(v[tmp] - per, v[j] - per) != 0))
{
ans[ansn][0] = i;
ans[ansn][1] = (i+1) % n;
if (ans[ansn][1] < ans[ansn][0]) swap(ans[ansn][0], ans[ansn][1]);
j = ansn;
ansn++;
while (j != 0 && ans[j][1] == ans[j-1][1] && ans[j][0] < ans[j-1][0])
{
swap(ans[j][0], ans[j-1][0]);
swap(ans[j][1], ans[j-1][1]);
j--;
}
}
}
printf("%d\n", ansn);
for (int i = 0; i < ansn; ++i)
printf("%.0lf %.0lf %.0lf %.0lf\n", v[ans[i][0]].x, v[ans[i][0]].y, v[ans[i][1]].x, v[ans[i][1]].y);
}
return 0;
}