白书板子考虑不周,上交小红书板子也不好,kuangbin的板子上的from UESTC的板子也有问题,网上那个题解抄kuangbin 的板子竟然也能A。最后我修正成了一个自己的板子
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<deque>
#define maxl 20010
#define eps 1e-8
const double inf=1e9;
using namespace std;
inline int sgn(double x)
{
if(x>-eps && x<eps) return 0;
if(x>0) return 1;
else return -1;
}
struct point
{
double x,y;
point(double a=0,double b=0)
{
x=a,y=b;
}
point operator + (const point &b)const
{
return point(x+b.x,y+b.y);
}
point operator - (const point &b)const
{
return point(x-b.x,y-b.y);
}
friend point operator * (const double &t,const point &a)
{
return point(t*a.x,t*a.y);
}
inline double norm()
{
return sqrt(x*x+y*y);
}
};
inline double dot(const point &a,const point &b)
{
return a.x*b.x+a.y*b.y;
}
inline double det(const point &a,const point &b)
{
return a.x*b.y-a.y*b.x;
}
struct halfplane
{// s->e on the left
point s,e;
double k;
halfplane(point a=point(),point b=point())
{
s=a;e=b;
k=atan2(e.y-s.y,e.x-s.x);
}
point operator &(const halfplane &b)const
{
double t=det(b.s-s,b.e-b.s);
t=t/det(e-s,b.e-b.s);
return s+t*(e-s);
}
};
inline bool satisfy(point a,const halfplane L)
{//不允许在线上
return sgn(det(a-L.s,L.e-L.s))<=0;
}
inline bool HPIcmp(const halfplane &a,const halfplane &b)
{//如果平行且同向,内侧的在前
int res=sgn(a.k-b.k);
return res==0 ? satisfy(a.s,b) : a.k<b.k;
}
halfplane Q[maxl];
void HPI(halfplane line[],int n,point res[],int &resn)
{
int tot=n;
sort(line,line+n,HPIcmp);
tot=1;
for(int i=1;i<n;i++)
if(sgn(line[i].k-line[i-1].k)!=0)
line[tot++]=line[i];
int head=0,tail=1;
Q[0]=line[0];
Q[1]=line[1];
resn=0;
for(int i=2;i<tot;i++)
{
if(sgn(det(Q[tail].e-Q[tail].s,Q[tail-1].e-Q[tail-1].s))==0||
sgn(det(Q[head].e-Q[head].s,Q[head+1].e-Q[head+1].s))==0)
return;
while(head<tail && !satisfy(Q[tail]&Q[tail-1],line[i]))
tail--;
while(head<tail && !satisfy(Q[head]&Q[head+1],line[i]))
head++;
Q[++tail]=line[i];
}
while(head<tail && !satisfy(Q[tail]&Q[tail-1],Q[head]))
tail--;
while(head<tail && !satisfy(Q[head]&Q[head+1],Q[tail]))
head++;
if(tail<=head+1) return;
for(int i=head;i<tail;i++)
res[resn++]=Q[i]&Q[i+1];
if(head<tail-1)
res[resn++]=Q[head]&Q[tail];
}
int n,cas,resn;
halfplane line[maxl];
point p[maxl],res[maxl];
double ans;
inline void prework()
{
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
if(i)
line[i]=halfplane(p[i],p[i-1]);
}
line[0]=halfplane(p[0],p[n-1]);
line[n++]=(halfplane(point(-inf,-inf),point(inf,-inf)));
line[n++]=(halfplane(point(inf,-inf),point(inf,inf)));
line[n++]=(halfplane(point(inf,inf),point(-inf,inf)));
line[n++]=(halfplane(point(-inf,inf),point(-inf,-inf)));
}
inline void mainwork()
{
HPI(line,n,res,resn);
}
inline void print()
{
++cas;
printf("Floor #%d\n",cas);
if(resn==0)
puts("Surveillance is impossible.");
else
puts("Surveillance is possible.");
puts("");
}
int main()
{
while(~scanf("%d",&n) && n)
{
prework();
mainwork();
print();
}
return 0;
}

本文深入探讨了凸包算法,一种用于解决计算机图形学和几何计算中常见问题的关键技术。通过详细解析算法的实现过程,包括点结构定义、半平面交求解、以及如何通过比较函数确定半平面顺序,为读者提供了全面的理解。此外,文章还分享了一个经过修正和优化的代码实例,展示了如何在实际应用中正确使用凸包算法。
234

被折叠的 条评论
为什么被折叠?



