K - 矩形面积 HDU - 5251
小度熊有一个桌面,小度熊剪了很多矩形放在桌面上,小度熊想知道能把这些矩形包围起来的面积最小的矩形的面积是多少。
Input
第一行一个正整数 T,代表测试数据组数(1≤T≤201≤T≤20),接下来 T 组测试数据。
每组测试数据占若干行,第一行一个正整数 N(1≤N<≤1000)N(1≤N<≤1000),代表矩形的数量。接下来 N 行,每行 8 个整数x1,y1,x2,y2,x3,y3,x4,y4x1,y1,x2,y2,x3,y3,x4,y4,代表矩形的四个点坐标,坐标绝对值不会超过10000。
Output
对于每组测试数据,输出两行:
第一行输出"Case #i:",i 代表第 i 组测试数据。
第二行包含1 个数字,代表面积最小的矩形的面积,结果保留到整数位。
Sample Input
2
2
5 10 5 8 3 10 3 8
8 8 8 6 7 8 7 6
1
0 0 2 2 2 0 0 2
Sample Output
Case #1:
17
Case #2:
4
给你n个矩形,每个矩形8个点是他的坐标
问你最小矩形覆盖住所有的矩形的面积是多少
#include <bits/stdc++.h>
using namespace std;
const int MAXN=1005;
const double eps=1e-10;
int dcmp(double x){
if(fabs(x)<eps)return 0;
if(x>0)return 1;
return -1;
} //弄精度
struct Point {
double x,y;
}p[MAXN]; //搞点
double dot(Point a,Point b,Point c){
double s1=b.x-a.x;
double t1=b.y-a.y;
double s2=c.x-a.x;
double t2=c.y-a.y;
return s1*s2+t1*t2;
} //点积
int n,res[MAXN],top;
bool cmp(Point a,Point b){
if(a.y==b.y)return a.x<b.x;
return a.y<b.y;
}
bool mult(Point sp,Point ep,Point op){
return (sp.x-op.x)*(ep.y-op.y)>=(ep.x-op.x)*(sp.y-op.y);
}
void Graham(){
int len;
top=1;
sort(p,p+n,cmp);
if(n==0)return;res[0]=0;
if(n==1)return;res[1]=1;
if(n==2)return;res[2]=2;
for(int i=2;i<n;i++){
while(top&&mult(p[i],p[res[top]],p[res[top-1]]))top--;
res[++top]=i;
}
len=top;
res[++top]=n-2;
for(int i=n-3;i>=0;i--){
while(top!=len&&mult(p[i],p[res[top]],p[res[top-1]]))top--;
res[++top]=i;
}
} //求凸包
double dist(Point a,Point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double Cross(Point a,Point b,Point c){
return (c.x-a.x)*(b.y-a.y) - (b.x-a.x)*(c.y-a.y);
}
double ComGeo(){
int R=1,U=1,L;
double ans=99999999999999;
for(int i=0;i<top;i++)
{
while(dcmp(fabs(Cross(p[res[i]],p[res[(i+1)%top]],p[res[(U+1)%top]]))-fabs(Cross(p[res[i]],p[res[(i+1)%top]],p[res[U]])))>=0)
U=(U+1)%top;//最上面一点
while(dcmp(dot(p[res[i]],p[res[(i+1)%top]],p[res[(R+1)%top]])-dot(p[res[i]],p[res[(i+1)%top]],p[res[R]]))>0)
R=(R+1)%top;//最右一点
if(i==0)L=R;
while(dcmp(dot(p[res[i]],p[res[(i+1)%top]],p[res[(L+1)%top]])-dot(p[res[i]],p[res[(i+1)%top]],p[res[L]]))<=0)
L=((L+1)%top)%top;//最左一点
//旋转卡壳精髓所在
double d=dist(p[res[i]],p[res[(i+1)%top]])*dist(p[res[i]],p[res[(i+1)%top]]);
double area=fabs(Cross(p[res[i]],p[res[(i+1)%top]],p[res[U]]))*//求面积
fabs(dot(p[res[i]],p[res[(i+1)%top]],p[res[R]])-dot(p[res[i]],p[res[(i+1)%top]],p[res[L]]))/d;
if(area<ans) ans = area;
}
return ans;
} //旋转卡壳
int main()
{
int t;
scanf("%d",&t);
int k=1;
while(t--){
int m;
scanf("%d",&m);
n=0;
double x;
for(int i=0;i<m;i++){
for(int j=1;j<=8;j++){
scanf("%lf",&x);
if(j&1)p[n].x=x;
else p[n++].y=x;
}
}
Graham();
double ans;
if(top<3)ans=0;
else
ans=ComGeo();
long long sum=ans+0.5;
printf("Case #%d:\n%I64d\n",k++,sum); //最后不要忘记四舍五入
}
return 0;
}
给你n个点,输出最小覆盖矩形和矩形的四个点
#include<bits/stdc++.h>
#define db double
using namespace std;
const int M=5e4+5;
const db eps=1e-8;
struct pt{db x,y;};
int sig(db x){return (x>eps)-(x<-eps);}
pt operator -(pt a,pt b){return (pt){a.x-b.x,a.y-b.y};}
pt operator +(pt a,pt b){return (pt){a.x+b.x,a.y+b.y};}
db operator *(pt a,pt b){return a.x*b.y-a.y*b.x;}
db operator /(pt a,pt b){return a.x*b.x+a.y*b.y;}
pt operator *(pt a,db b){return (pt){a.x*b,a.y*b};}
pt operator /(pt a,db b){return (pt){a.x/b,a.y/b};}
bool operator <(pt a,pt b){return !sig(a.y-b.y)?a.x<b.x:a.y<b.y;}
db area(pt a,pt b,pt c){return (b-a)*(c-a);}
db shad(pt a,pt b,pt c){return (b-a)/(c-a);}
db sqr(db x){return x*x;}
db dis(pt a,pt b){return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));}
bool cmp(pt a,pt b){return sig(a.x-b.x)?a.y<b.y:a.x<b.x;}
pt p[M],sta[M],rec[5];
int top,n;
void in()
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%lf%lf",&p[i].x,&p[i].y);
}
void tubao()
{
top=-1;
sort(p+1,p+1+n);
for(int i=1;i<=n;++i)
{
while(top>0&&sig(area(sta[top-1],sta[top],p[i]))<=0)--top;
sta[++top]=p[i];
}
int k=top;
for(int i=n-1;i>=1;--i)
{
while(top>k&&sig(area(sta[top-1],sta[top],p[i]))<=0)--top;
sta[++top]=p[i];
}
}
int x[M];
db fuck(db x){return !sig(x)?fabs(x):x;}
void ac()
{
int le=1,ri=1,up=1;
db L,R,H,D,tmp,ans=1e60;
for(int i=0;i<top;++i)
{
D=dis(sta[i],sta[i+1]);
while(sig(area(sta[i],sta[i+1],sta[up])-area(sta[i],sta[i+1],sta[up+1]))<=0)up=(up+1)%top;
while(sig(shad(sta[i],sta[i+1],sta[ri])-shad(sta[i],sta[i+1],sta[ri+1]))<=0)ri=(ri+1)%top;
if(i==0)le=up;
while(sig(shad(sta[i],sta[i+1],sta[le])-shad(sta[i],sta[i+1],sta[le+1]))>=0)le=(le+1)%top;
L=shad(sta[i],sta[i+1],sta[le])/D;
R=shad(sta[i],sta[i+1],sta[ri])/D;
H=area(sta[i],sta[i+1],sta[up])/D;
H=H>0?H:-H;
tmp=(R-L)*H;
if(tmp<ans)
{
ans=tmp;
rec[0]=sta[i]+(sta[i+1]-sta[i])*(R/D);
rec[1]=rec[0]+(sta[ri]-rec[0])*(H/dis(sta[ri],rec[0]));
rec[2]=rec[1]-(rec[0]-sta[i])*((R-L)/dis(sta[i],rec[0]));
rec[3]=rec[2]-(rec[1]-rec[0]);
}
}
ans=fabs(ans);
printf("%.5lf\n",ans);
int fir=0;
for(int i=1;i<=3;++i)
if(rec[i]<rec[fir])fir=i;
for(int i=0;i<=3;++i)
printf("%.5lf %.5lf\n",fuck(rec[(i+fir)%4].x),fuck(rec[(i+fir)%4].y));
}
int main()
{
in();tubao();
ac();
return 0;
}