计算几何 ( 判断线段相交 )——土地划分( FZU 1015 )

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>

using namespace std;
int W,H,L;
struct point
{
    double x;
    double y;
}Point[66];

struct line
{
    point s;
    point e;
}Line[100];

double multiply(point sp,point ep,point op)
{
    return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y));
}

bool intersect(line u,line v)
{
 return( (max(u.s.x, u.e.x)>=min(v.s.x, v.e.x))&&                     //排斥实验
   (max(v.s.x, v.e.x)>=min(u.s.x, u.e.x))&&
   (max(u.s.y, u.e.y)>=min(v.s.y, v.e.y))&&
   (max(v.s.y, v.e.y)>=min(u.s.y, u.e.y))&&
   (multiply(v.s, u.e, u.s)*multiply(u.e, v.e, u.s)>=0)&&         //跨立实验
   (multiply(u.s, v.e, v.s)*multiply(v.e, u.e, v.s)>=0));
}

int main()
{
    while(scanf("%d%d", &W, &H) && (W+H) )
    {
        cin >> L;
        point s,g;
        scanf("%lf%lf", &s.x, &s.y);
        for(int i=0; i<L; i++)
        {                             //l+1个点
            scanf("%lf%lf",&g.x,&g.y);
            Line[i].s=s;    
            Line[i].e=g;
            s=g;
        }

        int cnt =1;
        for(int i=0;i<L;i++)
        {
            for(int j=i+1;j<L;j++)
            {
                if (Line[i].e.x==Line[j].s.x && Line[i].e.y==Line[j].s.y ||
                    Line[i].s.x==Line[j].e.x && Line[i].s.y==Line[j].e.y) continue;

                if( intersect(Line[i], Line[j]))
                {
                    cnt++;
                    //cout << i << " : "<< j << endl;
                }
            }
        }
        int ans =cnt+L+1;
        if(Point[0].x == Point[L].x && Point[0].y == Point[L].y) ans--;
        cout << ans<< endl;
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值