凸包稳定性判断:每条边上是否至少有三点 POJ 1228

时间:2023-03-08 22:50:07
凸包稳定性判断:每条边上是否至少有三点    POJ 1228
 //凸包稳定性判断:每条边上是否至少有三点
// POJ 1228 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <math.h>
using namespace std;
#define LL long long
typedef pair<int,int> pii;
const double inf = 0x3f3f3f3f;
const LL MOD =100000000LL;
const int N =;
#define clc(a,b) memset(a,b,sizeof(a))
const double eps = 1e-;
void fre() {freopen("in.txt","r",stdin);}
void freout() {freopen("out.txt","w",stdout);}
inline int read() {int x=,f=;char ch=getchar();while(ch>''||ch<'') {if(ch=='-') f=-; ch=getchar();}while(ch>=''&&ch<='') {x=x*+ch-'';ch=getchar();}return x*f;} int sgn(double x){
if(fabs(x) < eps)return ;
if(x < )return -;
else return ;
} struct Point{
double x,y;
Point(){}
Point(double _x,double _y){
x = _x;y = _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;
}
double operator *(const Point &b)const{
return x*b.x + y*b.y;
}
friend bool operator<(const Point &a,const Point &b){
if(fabs(a.y-b.y)<eps) return a.x<b.x;
return a.y<b.y;
}
friend double dis2(Point a){
return a.x*a.x+a.y*a.y;
}
}; double dis(Point a,Point b){
return sqrt(dis2(a-b));
} int mult(Point a,Point b,Point o){
return sgn((a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y));
}
//返回top个点
int graham(Point p[],int n,Point q[]){
int top=;
sort(p,p+n);
if(n==) return ;
q[]=p[];
if(n==) return ;
q[]=p[];
if(n==) return ;
q[]=p[];
for(int i=;i<n;i++){
while(top&&(mult(p[i],q[top],q[top-])>)) top--;
q[++top]=p[i];
}
int len=top;
q[++top]=p[n-];
for(int i=n-;i>=;i--){
while(top!=len&&(mult(p[i],q[top],q[top-])>)) top--;
q[++top]=p[i];
}
return top;
} // 判断凸包边上是否至少有三点
bool judge(Point p[],int m){
p[m]=p[];
p[m+]=p[];
for(int i=;i<m;i++){
if(mult(p[i-],p[i+],p[i])!=&&mult(p[i],p[i+],p[i+])!=)
return false;
}
return true;
} Point p[];
Point ch[];
int main(){
// fre();
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
for(int i=;i<n;i++){
double x,y;
scanf("%lf%lf",&x,&y);
p[i]=Point(x,y);
}
int m=graham(p,n,ch);
if(n<) {
puts("NO");
continue;
}
if(judge(ch,m)) puts("YES");
else puts("NO");
}
return ;
}