poj 3335 rotating scoreboard

题解

用半平面交求多边形的内核 push边的时候应该是逆时针

代码

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

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-8;
const int maxp=105;


int (double x){
if(fabs(x)<eps) return 0;
if(x<0) return -1;
else return 1;
}

struct Point{
double x,y;
Point(){}
Point(double _x,double _y){
x = _x;
y = _y;
}

void input(){
scanf("%lf%lf",&x,&y);
}

Point operator - (const Point &b)const{
return Point(x-b.x,y-b.y);
}

double operator ^ (const Point &b)const{
return x*b.y - y*b.x;
}


};


struct Line{
Point s,e;
Line(){}
Line(Point _s,Point _e){
s = _s;
e = _e;
}

void input(){
s.input();
e.input();
}

bool parallel(Line v){
return sgn((e-s)^(v.e-v.s)) == 0;
}

Point crosspoint(Line v){
double a1 = (v.e-v.s)^(s-v.s);
double a2 = (v.e-v.s)^(e-v.s);
return Point((s.x*a2-e.x*a1)/(a2-a1),(s.y*a2-e.y*a1)/(a2-a1));
}

};

struct polygon {
int n;
Point p[maxp];
Line l[maxp];
void getline() {
for(int i=0;i<n;++i)
l[i] = Line(p[i],p[(i+1)%n]);
}
void input(int _n) {
n = _n;
for (int i=0;i<n;i++)
p[i].input();
}
};

struct halfplane:public Line{
double angle;
halfplane(){}
halfplane(Point _s,Point _e){
s = _s;
e = _e;
}
halfplane(Line v){
s = v.s;
e = v.e;
}
void calcangle(){
angle = atan2(e.y-s.y,e.x-s.x);
}

bool operator <(const halfplane &b)const{
return angle < b.angle;
}

};


struct halfplanes{
int n;
halfplane hp[maxp];
Point p[maxp];
int que[maxp];
int st,ed;
void push(halfplane tmp){
hp[n++] = tmp;
}

void unique(){
int m = 1;
for (int i = 1;i < n;i++)
{
if(sgn(hp[i].angle-hp[i-1].angle)!=0)
hp[m++] = hp[i];
else if(sgn((hp[m-1].e-hp[m-1].s) ^ (hp[i].s-hp[m-1].s)) > 0)
hp[m-1] = hp[i];
}
n = m;
}

bool halfplaneinsert(){
for (int i=0; i < n;i++)
hp[i].calcangle();
sort(hp,hp+n);
unique();
que[st=0] = 0;
que[ed=1] = 1;

p[1] = hp[0].crosspoint(hp[1]);
for (int i=2;i<n;i++)
{
while (st<ed && sgn((hp[i].e-hp[i].s) ^(p[ed]-hp[i].s))<0 )
ed--;
while (st<ed && sgn((hp[i].e-hp[i].s)^(p[st+1]-hp[i].s))<0)
st++;
que[++ed]=i;
if(hp[i].parallel(hp[que[ed-1]]))
return false;
p[ed]=hp[i].crosspoint(hp[que[ed-1]]);
}

while (st<ed && sgn((hp[que[st]].e-hp[que[st]].s) ^(p[ed]-hp[que[st]].s))<0 )
ed--;
while (st<ed && sgn((hp[que[ed]].e-hp[que[ed]].s)^(p[st+1]-hp[que[ed]].s))<0)
st++;

if(st+1 >=ed) return false;
return true;
}

};

int main()
{
int T;
scanf("%d",&T);
while (T--)
{
int n;
scanf("%d",&n);
halfplanes A;
polygon pol;
pol.input(n);
pol.getline();
A.n=0;

for (int i=0;i<n;i++)
{
Line line = pol.l[i];
swap(line.s,line.e);
A.push(halfplane(line));
}
// A.unique();
if(A.halfplaneinsert())
puts("YES");
else puts("NO");
}
return 0;
}