hdu-You can Solve a Geometry Problem too

http://acm.hdu.edu.cn/showproblem.php?pid=1086     

题意:判断两两直线相交的个数

code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
//点
struct POINT
{
    double x, y;
    POINT(){ }
    POINT(double a, double b){
        x = a;
        y = b;
    }
}p[105];
//线段
struct Seg
{
    POINT a, b;
    Seg() { }
    Seg(POINT x, POINT y){
        a = x;
        b = y;
    }
};
//叉乘
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);
}
//判断点在线段上
bool On_Seg(POINT a, Seg s)
{
    double maxx = max(s.a.x, s.b.x), minx = min(s.a.x, s.b.x);
    double maxy = max(s.a.y, s.b.y), miny = min(s.a.y, s.b.y);
    if(a.x >= minx && a.x <= maxx && a.y >= miny && a.y <= maxy) return true;
    return false;
}
//判断线段相交
bool Seg_cross(Seg s1, Seg s2)
{
    double cs1 = cross(s1.a, s2.a, s2.b);
    double cs2 = cross(s1.b, s2.a, s2.b);
    double cs3 = cross(s2.a, s1.a, s1.b);
    double cs4 = cross(s2.b, s1.a, s1.b);
    // 互相跨立
    if(cs1 * cs2 < 0 && cs3 * cs4 < 0) return true;
    if(cs1 == 0 && On_Seg(s1.a, s2)) return true;
    if(cs2 == 0 && On_Seg(s1.b, s2)) return true;
    if(cs3 == 0 && On_Seg(s2.a, s1)) return true;
    if(cs4 == 0 && On_Seg(s2.b, s1)) return true;
    return false;
}

int main(){
    int n;
    while(cin >> n, n){
        Seg p[105];
        for(int i = 0;i < n;i++)
            cin >> p[i].a.x >> p[i].a.y >> p[i].b.x >> p[i].b.y;
        int cnt = 0;
        for(int i = 0;i < n;i++)
        {
            for(int j = i+1;j < n;j++)
            {
                if(Seg_cross(p[i],p[j])) cnt ++;
            }
        }
        cout << cnt << endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值