CSU 1812 三角形和矩形 【几何】

Description

Bobo 有一个三角形和一个矩形,他想求他们交的面积。
具体地,三角形和矩形由 8 个整数 x  1,y  1,x  2,y  2,x  3,y  3,x  4,y  4 描述。 表示三角形的顶点坐标是 (x  1,y  1),(x  1,y 2),(x  2,y  1), 矩形的顶点坐标是 (x  3,y  3),(x  3,y  4),(x  4,y  4),(x  4,y  3).

Input

输入包含不超过 30000 组数据。
每组数据的第一行包含 4 个整数 x  1,y  1,x  2,y  2 (x  1≠x  2,y  1≠y  2).
第二行包含 4 个整数 x  3,y  3,x  4,y  4 (x  3<x  4,y  3<y  4).
(0≤x  i,y  i≤10  4)

Output

对于每组数据,输出一个实数表示交的面积。绝对误差或相对误差小于 10  -6 即认为正确。

Sample Input

1 1 3 3
0 0 2 2
0 3 3 1
0 0 2 2
4462 1420 2060 2969
4159 257 8787 2970

Sample Output

1.00000000
0.75000000
439744.13967527

Hint


直接套多边形相交面积模版就行了



#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define maxn 510
const double eps=1E-8;
int sig(double d){
    return(d>eps)-(d<-eps);
}
struct Point{
    double x,y; Point(){}
    Point(double x,double y):x(x),y(y){}
    bool operator==(const Point&p)const{
        return sig(x-p.x)==0&&sig(y-p.y)==0;
    }
};
double cross(Point o,Point a,Point b){
    return(a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
}
double area(Point* ps,int n){
    ps[n]=ps[0];
    double res=0;
    for(int i=0;i<n;i++){
        res+=ps[i].x*ps[i+1].y-ps[i].y*ps[i+1].x;
    }
    return res/2.0;
}
int lineCross(Point a,Point b,Point c,Point d,Point&p){
    double s1,s2;
    s1=cross(a,b,c);
    s2=cross(a,b,d);
    if(sig(s1)==0&&sig(s2)==0) return 2;
    if(sig(s2-s1)==0) return 0;
    p.x=(c.x*s2-d.x*s1)/(s2-s1);
    p.y=(c.y*s2-d.y*s1)/(s2-s1);
    return 1;
}

void polygon_cut(Point*p,int&n,Point a,Point b){
    static Point pp[maxn];
    int m=0;p[n]=p[0];
    for(int i=0;i<n;i++){
        if(sig(cross(a,b,p[i]))>0) pp[m++]=p[i];
        if(sig(cross(a,b,p[i]))!=sig(cross(a,b,p[i+1])))
            lineCross(a,b,p[i],p[i+1],pp[m++]);
    }
    n=0;
    for(int i=0;i<m;i++)
        if(!i||!(pp[i]==pp[i-1]))
            p[n++]=pp[i];
    while(n>1&&p[n-1]==p[0])n--;
}

double intersectArea(Point a,Point b,Point c,Point d){
    Point o(0,0);
    int s1=sig(cross(o,a,b));
    int s2=sig(cross(o,c,d));
    if(s1==0||s2==0)return 0.0;
    if(s1==-1) swap(a,b);
    if(s2==-1) swap(c,d);
    Point p[10]={o,a,b};
    int n=3;
    polygon_cut(p,n,o,c);
    polygon_cut(p,n,c,d);
    polygon_cut(p,n,d,o);
    double res=fabs(area(p,n));
    if(s1*s2==-1) res=-res;return res;
}

double intersectArea(Point*ps1,int n1,Point*ps2,int n2){
    if(area(ps1,n1)<0) reverse(ps1,ps1+n1);
    if(area(ps2,n2)<0) reverse(ps2,ps2+n2);
    ps1[n1]=ps1[0];
    ps2[n2]=ps2[0];
    double res=0;
    for(int i=0;i<n1;i++){
        for(int j=0;j<n2;j++){
            res+=intersectArea(ps1[i],ps1[i+1],ps2[j],ps2[j+1]);
        }
    }
    return res;
}
Point ps1[maxn],ps2[maxn];
int n1,n2;
int main(){
    n1=3;n2=4;
    double x1,y1,x2,y2,x3,y3,x4,y4;
    while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4)!=EOF){
        ps1[0].x=x1,ps1[0].y=y1;
        ps1[1].x=x1,ps1[1].y=y2;
        ps1[2].x=x2,ps1[2].y=y1;
        ps2[0].x=x3,ps2[0].y=y3;
        ps2[1].x=x3,ps2[1].y=y4;
        ps2[2].x=x4,ps2[2].y=y4;
        ps2[3].x=x4,ps2[3].y=y3;

        double ans=intersectArea(ps1,n1,ps2,n2);
        printf("%.8f\n",ans);
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值