zoukankan      html  css  js  c++  java
  • 判断线段相交

    #include <iostream>
    #include <cmath>
    #include <cstdio>
    using namespace std;
    struct line{
        double x1,y1;
        double x2,y2;
        bool operator < (const line &a) const
        {
           return x1 < a.x1;
        }
    }arr[110];
    bool cross(line a,line b)
    {
        double fa=((a.x2-a.x1)*(b.y1-a.y1)-(a.y2-a.y1)*(b.x1-a.x1))*
            ((a.x2-a.x1)*(b.y2-a.y1)-(a.y2-a.y1)*(b.x2-a.x1));
        if(fa>0) return false;
    
        double fb=((b.x2-b.x1)*(a.y1-b.y1)-(b.y2-b.y1)*(a.x1-b.x1))*
            ((b.x2-b.x1)*(a.y2-b.y1)-(b.y2-b.y1)*(a.x2-b.x1));
        if(fb>0) return false;
        if(fa==0&&fb==0)
        if( (a.x1-b.x1)*(a.x2-b.x1)+(a.y1-b.y1)*(a.y2-b.y1)>0 &&
            (a.x1-b.x2)*(a.x2-b.x2)+(a.y1-b.y2)*(a.y2-b.y2)>0 )
            return false;
        return true;
    }
    int main()
    {
        int n;
        while(cin>>n,n)
        {
            for(int i=0;i<n;i++)
            {
                scanf("%lf%lf%lf%lf",&arr[i].x1,&arr[i].y1,&arr[i].x2,&arr[i].y2);
            }
            int ans=0;
            for(int i=0;i<n;i++)
            for(int j=i+1;j<n;j++)
            if(cross(arr[i],arr[j])) ans++;
            cout<<ans<<endl;
        }
        return 0;
    }
    /*
    2
    0 0 0 2
    0 3 0 5
    2
    0 0 0 2
    0 2 4 0
    
    */
  • 相关阅读:
    Sublime Text配置Python开发利器
    Python字符进度条
    安装和使用的django的debug_toolbar
    Python数组合并
    django创建项目
    Python的闭包
    Python获取对象的元数据
    Python的枚举类型
    Django的Model上都有些什么
    Git使用相关
  • 原文地址:https://www.cnblogs.com/qijinbiao/p/2614947.html
Copyright © 2011-2022 走看看