uva 1453 - Squares

时间:2023-03-09 03:30:30
uva 1453 - Squares

旋转卡壳算法;

直接在这个上面粘的模板

主要用途:用于求凸包的直径、宽度,两个不相交凸包间的最大距离和最小距离···

这题就是求凸包的直径

 #include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#define eps 1e-9
using namespace std;
const double pi = acos(-); int dcmp(double x)
{
return fabs(x) < eps ? : (x > ? : -);
} struct Point
{
double x;
double y; Point(double x = , double y = ):x(x), y(y) {} bool operator < (const Point& e) const
{
return dcmp(x - e.x) < || (dcmp(x - e.x) == && dcmp(y - e.y) < );
} bool operator == (const Point& e) const
{
return dcmp(x - e.x) == && dcmp(y - e.y) == ;
}
}; typedef Point Vector; Vector operator + (Point A, Point B)
{
return Vector(A.x + B.x, A.y + B.y);
} Vector operator - (Point A, Point B)
{
return Vector(A.x - B.x, A.y - B.y);
} Vector operator * (Point A, double p)
{
return Vector(A.x * p, A.y * p);
} Vector operator / (Point A, double p)
{
return Vector(A.x / p, A.y / p);
}
double dot(Point a,Point b)
{
return a.x*b.x+a.y*b.y;
}
double cross(Point a,Point b)
{
return a.x*b.y-a.y*b.x;
}
Point rotate(Point a,double ang)
{
return Point(a.x*cos(ang)-a.y*sin(ang),a.x*sin(ang)+a.y*cos(ang));
}
int convexhull(Point *p,int n,Point *ch)
{
sort(p,p+n);
int m=;
for(int i=; i<n; i++)
{
while(m>&&cross(ch[m-]-ch[m-],p[i]-ch[m-])<=)m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-; i>=; i--)
{
while(m>k&&cross(ch[m-]-ch[m-],p[i]-ch[m-])<=)m--;
ch[m++]=p[i];
}
if(n>)m--;
return m;
}
bool onsegment(Point p,Point a,Point b)
{
return dcmp(cross(a-p,b-p))==&&dcmp(dot(a-p,b-p))<;
} bool SegmentProperIntersection( Point a1, Point a2, Point b1, Point b2 ) //线段相交,交点不在端点
{
double c1 = cross( a2 - a1, b1 - a1 ), c2 = cross( a2 - a1, b2 - a1 ),
c3 = cross( b2 - b1, a1 - b1 ), c4 = cross( b2 - b1, a2 - b1 );
return dcmp(c1)*dcmp(c2) < && dcmp(c3) * dcmp(c4) < ;
} int ispointinpolygon(Point p,int n,Point *poly)
{
int wn=;
for(int i=; i<n; i++)
{
if(onsegment(p,poly[i],poly[(i+)%n]))return -;
int k=dcmp(cross(poly[(i+)%n]-poly[i],p-poly[i]));
int d1=dcmp(poly[i].y-p.y);
int d2=dcmp(poly[(i+)%n].y-p.y);
if(k>&&d1<=&&d2>)wn++;
if(k<&&d2<=&&d1>)wn--;
}
if(wn!=)return ;
return ;
} bool Check(int n,Point *ch,int m,Point *th)
{
for(int i=; i<n; i++)
{
if(ispointinpolygon(ch[i],m,th)!=)return ;
}
for(int i=; i<m; i++)
if(ispointinpolygon(th[i],n,ch)!=)return ;
ch[n]=ch[];
th[m]=th[];
for(int i=; i<n; i++)
for(int j=; j<m; j++)
if(SegmentProperIntersection(ch[i],ch[i+],th[j],th[j+]))return ;
return ;
}
double rotating_calipers(Point *ch,int n)
{
int q=;
double ans=;
ch[n]=ch[];
for ( int i = ; i < n; ++i )
{
while ( cross( ch[i + ] - ch[i], ch[q + ] - ch[i] ) > cross( ch[i + ] - ch[i], ch[q] - ch[i] ) )
q = ( q + ) % n;
ans = max( ans, max( dot( ch[i]- ch[q],ch[i]-ch[q] ),dot( ch[i + ]-ch[q + ],ch[i + ]-ch[q + ] ) ));
}
return ans;
}
Point p[],ch[];
int main()
{
int n,m;
double x,y,w;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int cnt=;
for(int i=; i<n; i++)
{
scanf("%lf%lf%lf",&x,&y,&w);
p[cnt].x=x,p[cnt++].y=y;
p[cnt].x=x+w,p[cnt++].y=y;
p[cnt].x=x,p[cnt++].y=y+w;
p[cnt].x=x+w,p[cnt++].y=y+w;
}
int n1=convexhull(p,cnt,ch);
printf("%.0lf\n",rotating_calipers(ch,n1));
}
return ;
}