zoukankan      html  css  js  c++  java
  • Intersection

    题意:判断一条线段是否在矩形里或与矩形相交。

    思路:先判断线段的两个点是否在矩形里,再判断线段是否与矩形的四条边相交即可。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    typedef long long ll;
    ll chaj(ll x1,ll y1,ll x2,ll y2)
    {
        ll ans=x1*y2-x2*y1;
        if(ans==0)
            return 0;
        if(ans>0)
            return 1;
        return -1;
    }
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            ll x1,y1,x2,y2,x3,y3,x4,y4;
            cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
            if(x1>=min(x3,x4)&&x1<=max(x3,x4)&&y1>=min(y3,y4)&&y1<=min(y3,y4))
            {
                cout<<"T"<<endl;
                continue;
            }
            if(x2>=min(x3,x4)&&x2<=max(x3,x4)&&y2>=min(y3,y4)&&y2<=min(y3,y4))
            {
                cout<<"T"<<endl;
                continue;
            }
            int s=0;
            if( chaj(x1-x3,y1-y3,0,y4-y3)*chaj(x2-x3,y2-y3,0,y4-y3)<=0&&chaj(x3-x1,y3-y1,x2-x1,y2-y1)*chaj(x3-x1,y4-y1,x2-x1,y2-y1)<=0  )
                s=1;
            if( chaj(x1-x3,y1-y3,x4-x3,0)*chaj(x2-x3,y2-y3,x4-x3,0)<=0&&chaj(x3-x1,y3-y1,x2-x1,y2-y1)*chaj(x4-x1,y3-y1,x2-x1,y2-y1)<=0  )
                s=1;
            if( chaj(x1-x4,y1-y4,x3-x4,0)*chaj(x2-x4,y2-y4,x3-x4,0)<=0&&chaj(x4-x1,y4-y1,x2-x1,y2-y1)*chaj(x3-x1,y4-y1,x2-x1,y2-y1)<=0  )
                s=1;
            if( chaj(x1-x4,y1-y4,0,y3-y4)*chaj(x2-x4,y2-y4,0,y3-y4)<=0&&chaj(x4-x1,y4-y1,x2-x1,y2-y1)*chaj(x4-x1,y3-y1,x2-x1,y2-y1)<=0  )
                s=1;
            if(s)
                cout<<"T"<<endl;
            else
                cout<<"F"<<endl;
        }
    }
  • 相关阅读:
    hdu 1690
    hdu 2962
    hdu 2923
    hdu 1385
    hdu 1505
    python:获取访问访问时的响应时间
    自动化脚本编写环境部署_win7(RF)
    python操作浏览器及截图小结
    Python + Selenium 实现对页面的指定元素截图(可截长图元素)【转载】
    Linux:DNS服务器搭建及配置
  • 原文地址:https://www.cnblogs.com/zcb123456789/p/13669688.html
Copyright © 2011-2022 走看看