Rotating Scoreboard POJ - 3335
半平面交裸题:
亮模板:
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN=1e3+5;
const double EPS=1e-8;
const double PI=acos(-1);
inline int sgn(double a){ return a < -EPS ? -1 : a > EPS; }
inline int cmp(double a, double b){ return sgn(a-b); }
long long mod=1e9+7;
int n,m,T;
struct Point;
struct Line;
typedef Point Vector;
struct Point{
double x,y;
Point(){}
Point(double a, double b):x(a),y(b){}
double len(){return sqrt(x*x+y*y);}
void read(){scanf("%lf%lf",&x,&y);}
Point operator+(Vector v){return {x+v.x,y+v.y};}
Vector operator-(Point p){return {x-p.x,y-p.y};}
double operator^(Vector v){return x*v.y-y*v.x;}//叉乘
double operator*(Vector v){return x*v.x+y*v.y;}//点乘
Vector operator*(double d){return {x*d,y*d};}
Vector operator/(double d){return {x/d,y/d};}
bool operator==(Point p){return cmp(x,p.x)==0&&cmp(y,p.y)==0;}
bool operator<(Point p){if(cmp(x,p.x)==0) return y<p.y;return x<p.x;}
};
struct Line{
Line(){}
Line(Point a,Point b){s=a;e=b;ang=atan2(e.y-s.y,e.x-s.x);}
Point operator &(Line &b){
double t=((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
return s+(e-s)*t;
}
Point s,e;
double ang;
};
bool HPIcmp(Line a,Line b) {
if (fabs(a.ang - b.ang) > EPS)return a.ang < b.ang;
else return ((a.s - b.s) ^ (b.e - b.s)) < 0;
}
struct halfplanes{
int n;
Line line[MAXN];
Point p[MAXN];
Line Q[MAXN];
int head,tail;
void read(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
p[i].read();
}
void connect(){
for(int i=1;i<=n;i++)
line[i]={p[i],p[i%n+1]};
}
bool HPI() {
sort(line+1, line+n+1, HPIcmp);
int tot = 1;
for (int i = 2; i <= n; i++)
if (fabs(line[i].ang - line[i - 1].ang) > EPS) //去掉斜率重复的
line[++tot] = line[i];
head = 1, tail = 2;
Q[1] = line[1];Q[2] = line[2];
for (int i = 3; i <= tot; i++) {
if (fabs((Q[tail].e - Q[tail].s) ^ (Q[tail - 1].e - Q[tail - 1].s)) < EPS ||
fabs((Q[head].e - Q[head].s) ^ (Q[head + 1].e - Q[head + 1].s)) < EPS)
return false;
while (head < tail && (((Q[tail] & Q[tail - 1]) -
line[i].s) ^ (line[i].e - line[i].s)) > EPS)
tail--;
while (head < tail && (((Q[head] & Q[head + 1]) -
line[i].s) ^ (line[i].e - line[i].s)) > EPS)
head++;
Q[++tail] = line[i];
}
while (head < tail && (((Q[tail] & Q[tail - 1]) -
Q[head].s) ^ (Q[head].e - Q[head].s)) > EPS)
tail--;
while (head < tail && (((Q[head] & Q[head + 1]) -
Q[tail].s) ^ (Q[tail].e - Q[tail].s)) > EPS)
head++;
if (tail <= head + 1) return false;
return true;
}
void getConvex(Point res[], int &resn){//得到核上的点
resn=0;
for(int i = head; i < tail; i++)
res[++resn] = Q[i]&Q[i+1];
if(head < tail - 1)
res[++resn] = Q[head]&Q[tail];
}
}hpi;
bool judge(Point p[],int n) {
double ans = 0;
for (int i = 1; i <= n; i++) {
ans += (p[i] ^ p[i % n + 1]);
}
return ans<0;
}
void solve(){
if(hpi.HPI()) printf("YES\n");
else printf("NO\n");
}
void init(){
hpi.read();
if(judge(hpi.p,hpi.n))//判断点的输入顺序,顺时针要反转
reverse(hpi.p+1,hpi.p+hpi.n+1);
hpi.connect();
}
int main(){
scanf("%d",&T);
while(T--){
init();
solve();
}
return 0;
}