题目链接:http://192.168.2.240:8080/JudgeOnline/showproblem?problem_id=1965
polygon半平面交 Time Limit:1000MS Memory Limit:165536K Description Input n为半平面个数,以下n行表示若干半平面。形式如ax+by+c<=0 Output 输出半平面交的面积。保留3位小数 Sample Input 4 Sample Output 4.000 Source 计算几何 半平面交 |
半平面交裸题。
讲道理不是说好保留三位小数吗,TMD数据全是保留到整数……幸好我WA了一次之后看了看数据,不然不知道要改多久……机智如我
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#define inf 1e10
#define maxn 10010
using namespace std;
int n,head,tail,tot,cnt;
double ans;
const double eps=1e-;
struct point{double x,y;}p[maxn];
point operator +(point x,point y){return (point){x.x+y.x,x.y+y.y};}
point operator -(point x,point y){return (point){x.x-y.x,x.y-y.y};}
struct line{double ang,a,b,c;point pt;}li[maxn],que[maxn];
double dot(point a,point b){return a.x*b.x+a.y*b.y;}
double cross(point x,point y,point z){return (x.x-z.x)*(y.y-z.y)-(x.y-z.y)*(y.x-z.x);}
bool includ(line x,point y){return y.x*x.a+y.y*x.b+x.c<=eps;}
bool comp(line x,line y){
if(x.ang==y.ang)return includ(y,x.pt);
return x.ang<y.ang;
}
point calc(line s1,line s2){
double v1=s1.b*s2.c-s1.c*s2.b,v2=s1.c*s2.a-s1.a*s2.c;
double v0=s1.a*s2.b-s1.b*s2.a;
return (point){v1/v0,v2/v0};
}
bool check(line x,line y,line z){return !includ(z,calc(x,y));}
bool solve(){
head=;tail=;
for(int i=;i<=tot;i++){
if(i>&&fabs(li[i].ang-li[i-].ang)<=eps)continue;
while(head<tail&&check(que[tail-],que[tail],li[i]))tail--;
while(head<tail&&check(que[head],que[head+],li[i]))head++;
que[++tail]=li[i];
}
while(head<tail&&check(que[tail-],que[tail],que[head]))tail--;
while(head<tail&&check(que[head],que[head+],que[tail]))head++;
for(int i=head;i<tail;i++)p[++cnt]=calc(que[i],que[i+]);
p[++cnt]=calc(que[head],que[tail]);
p[cnt+]=p[];
}
void getans(){
ans=;for(int i=;i<=cnt;i++)ans+=cross(p[i],p[i+],(point){,});ans=fabs(ans)/;
}
int main(){
//freopen("polygon.in","r",stdin);
//freopen("polygon.out","w",stdout);
scanf("%d",&n);
for(int i=;i<=n;i++){
double x,y,z;
scanf("%lf%lf%lf",&x,&y,&z);
li[++tot].a=x;li[tot].b=y;li[tot].c=z;
}
++tot,li[tot].a=-,li[tot].b=,li[tot].c=-inf;
++tot,li[tot].a=,li[tot].b=,li[tot].c=-inf;
++tot,li[tot].a=,li[tot].b=-,li[tot].c=-inf;
++tot,li[tot].a=,li[tot].b=,li[tot].c=-inf;
for(int i=;i<=tot;i++){
li[i].ang=atan2(li[i].b,li[i].a);
if(li[i].b) li[i].pt=(point){,-li[i].c/li[i].b};
else li[i].pt=(point){-li[i].c/li[i].a,};
}
sort(li+,li+tot+,comp);
solve();getans();
printf("%.0lf\n",ans);
return ;
}