zoj 1648 Grandpa's Estate(判断线段是否相交(不考虑端点相交))

题目地址

题目大意:给出n及n条线段的2个端点坐标,求是否有线段相交(不考虑端点相交)

解题思路:用差积判断线段的起点与另一线段的2个端点分别构成的向量是否分别在该线段向量的2边

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <list>

using namespace std;

const int maxn = 2000+10;
const double eps = 1e-10;

int dcmp(double x)
{
    if(fabs(x) < eps)    return 0;
    return x < 0 ? -1 : 1;
}

struct Point
{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}
    bool operator == (const Point& A)const
    {
        return dcmp(x-A.x)==0 && dcmp(y-A.y)==0;
    }
    bool operator < (const Point& A)const
    {
        return dcmp(x-A.x)<0 || dcmp(x-A.x)==0;
    }
}p1[maxn],p2[maxn];

typedef Point Vector;

Vector operator - (Point A,Point B)
{
    return Vector(A.x-B.x,A.y-B.y);
}

double Cross(Vector A,Vector B)
{
    return A.x*B.y-A.y*B.x;
}

bool isInter(Point A1,Point A2,Point B1,Point B2)
{
    double c1 = Cross(A2-A1,B1-A1);
    double c2 = Cross(A2-A1,B2-A1);
    double c3 = Cross(B2-B1,A1-B1);
    double c4 = Cross(B2-B1,A2-A1);
    return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;
}

int main()
{
    int n;
    while(scanf("%d",&n) != EOF)
    {
        for(int i = 0; i < n; i++)
        {
            scanf("%lf%lf%lf%lf",&p1[i].x,&p1[i].y,&p2[i].x,&p2[i].y);
        }
        int flag = 0;
        for(int i = 0; i < n; i++)
        {
            for(int j = i+1; j < n; j++)
            {
                if(isInter(p1[i],p2[i],p1[j],p2[j]))
                {
                    printf("burned!\n");
                    flag = 1;
                    break;
                }
            }
            if(flag)    break;
        }
        if(!flag)   printf("ok!\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值