斩bnuoj 直线与矩形的交点

Time Limit: 1000ms
Memory Limit: 65536KB
64-bit integer IO format:  %lld      Java class name:  Main
Type: 
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •                   
  • 在电影里面,我们经常可以看到武士们拔出刀,然后一刀斩下去,结果………………………………一张纸片被砍成了两半,囧…………

    而在本题中,我们需要计算一下被斩下去较小的那一部分的面积。

    我们假设纸片是矩形的,平行于坐标轴的,武士砍纸片的轨迹是一条直线。

    Input

    第一行一个整数N(2<=N<=350),表示数据组数。

    接下来一行,每行7个整数,xl,yl,xr,yr,a,b,c ,表分别表示矩形左下角坐标(xl,yl),右上角坐标(xr,yr),以及轨迹方程ax+by+c=0。整数的绝对值均小于200。

    Output

    对于每一组数据,输出一个三位小数,表示砍后较小那部分的面积。数据保证这个面积大于0.001。

    Sample Input

    3
    1 1 3 3 -1 1 0
    1 1 3 4 -1 1 0
    1 2 3 4 -1 1 0

    Sample Output

    2.000
    2.000
    0.500
    分析:按照斜率讨论交点的情况太麻烦容易出错,按照交点讨论更加方便;

    code:

    <span style="font-size:18px;">#include<iostream>
    #include<algorithm>
    #include<stdio.h>
    #include<math.h>
    using namespace std;
    int main()
    {
        int t;
        int xl,yl,xr,yr,a,b,c;
        double xtl,ytl,xtr,ytr;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d%d%d%d%d%d",&xl,&yl,&xr,&yr,&a,&b,&c);
            double ans=0;
            double tot=(xr-xl)*(yr-yl);
        //求出线段与各边  可能  存在的交点
            xtl=(-c-a*xl)*1.0/b;
            ytl=(-c-b*yl)*1.0/a;
            xtr=(-c-a*xr)*1.0/b;
            ytr=(-c-b*yr)*1.0/a;
    
        //判断上述所求交点是否在矩形各边上;
            int xlt=(xtl<=yr && xtl>=yl);
            int ylt=(ytl<=xr && ytl>=xl);
            int xrt=(xtr<=yr && xtr>=yl);
            int yrt=(ytr<=xr && ytr>=xl);
    <span style="white-space:pre">	</span>//矩形
            if(ylt&&yrt)
                {
                ans=(ytl+ytr-2*xl)*(yr-yl)*0.5;
                ans=min(ans,tot-ans);
                //cout<<are<<"%%"<<ans<<endl;
                }
    
            else if(xlt&&xrt)
            {
                ans=(xtl+xtr-2*yl)*(xr-xl)*0.5;
                ans=min(ans,tot-ans);
            }
    <span style="white-space:pre">	</span>//三角形
            else if(ylt&&xrt&&ytl!=xr)
                 ans=(xr-ytl)*(xtr-yl)/2.0;
    
            else if(xlt&&yrt&&ytr!=xl)
                ans=(ytr-xl)*(yr-xtl)/2.0;
    
            else if(yrt&&xrt&&ytr!=xr)
                ans=(xr-ytr)*(yr-xtr)/2.0;
    
           else if(xlt&&ylt&&ytl!=xl)
                ans=(ytl-xl)*(xtl-yl)/2.0;
    
            printf("%.3lf\n",ans);
        }
        return 0;
    }</span>



    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值