hdu 1086 You can Solve a Geometry Problem too

题目:

B - You can Solve a Geometry Problem too

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is very easy, after all we are now attending an exam, not a contest :) 
Give you N (1<=N<=100) segments(线段), please output the number of all intersections(交点). You should count repeatedly if M (M>2) segments intersect at the same point. 

Note: 
You can assume that two segments would not intersect at more than one point. 

 

Input

Input contains multiple test cases. Each test case contains a integer N (1=N<=100) in a line first, and then N lines follow. Each line describes one segment with four float values x1, y1, x2, y2 which are coordinates of the segment’s ending. 
A test case starting with 0 terminates the input and this test case is not to be processed. 

 

Output

For each case, print the number of intersections, and one line one case. 

 

Sample Input

2

0.00 0.00 1.00 1.00

0.00 1.00 1.00 0.00

3

0.00 0.00 1.00 1.00

0.00 1.00 1.00 0.000

0.00 0.00 1.00 0.00

Sample Output

1

题目大意:

给n个线段,求这些线段是否相交于一点(不会完全重合或者部分重合),且每个相交的点可以重复计算.

题目思路:

1、题目是考察线段是否相交,不考察相交的点是否重合

2、那么我们可以用向量的排斥试验和跨立实验来验证

已知两条线段P1P2和Q1Q2,判断P1P2和Q1Q2是否相交,若相交,求出交点。

两条线段的位置关系可以分为三类:有重合部分、无重合部分但有交点、无交点。

算法核心

算法的步骤如下:

1.快速排斥实验。

设以线段P1P2为对角线的矩形为R,设以线段Q1Q2为对角线的矩形为T,如果R和T不相交,则两线段不相交。

所以P1P2和Q1Q2相交的必要条件是以他们为对角线的矩形相交,即:

min(p1.x,p2.x) <= max(q1.x,q2.x) &&

min(q1.x,q2.x) <= max(p1.x,p2.x) &&

min(p1.y,p2.y) <= max(q1.y,q2.y) &&

min(q1.y,q2.y) <= max(p1.y,p2.y);

2.跨立实验。

如果两线段相交,则两线段必然相互跨立对方。
线段的跨立究竟是什么意思?向量的跨立是什么意思?
a、若P1P2跨立Q1Q2,则矢量(P1-Q1)(P2-Q1)位于矢量(Q2-Q1)的两侧,即( P1 - Q1 ) × ( Q2 - Q1 ) * ( P2 - Q1 ) × ( Q2 - Q1 ) < 0

等价于
(Q1.x-P1.x,Q1.y-P1.y) × ( Q1.x-Q2.x,Q1.y-Q2.y ) * ( Q1.x-P2.x,Q1.y-P2.y ) × ( Q1.x-Q2.x,Q1.y-Q2.y ) < 0
等价于
((Q1.x-P1.x)*(Q1.y-Q2.y)-(Q1.y-P1.y)*( Q1.x-Q2.x)) * ((Q1.x-P2.x)*(Q1.y-Q2.y)-(Q1.y-P2.y)*(Q1.x-Q2.x)) < 0

b、若Q1Q2跨立P1P2,则矢量(Q1-P1)(Q2-P1)位于矢量(P2-P1)的两侧,即( Q1 - P1 ) × ( P2 - P1 ) * ( Q2 - P1 ) × ( P2 - P1 ) < 0
等价于
(P1.x-Q1.x,P1.y-Q1.y) × ( P1.x-P2.x,P1.y-P2.y ) * ( P1.x-Q2.x,P1.y-Q2.y ) × ( P1.x-P2.x,P1.y-P2.y ) < 0
等价于
((P1.x-Q1.x)*(P1.y-P2.y)-(P1.y-Q1.y)*(P1.x-P2.x)) * ((P1.x-Q2.x)*(P1.y-P2.y)-(P1.y-Q2.y)*( P1.x-P2.x)) < 0

程序:

 

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cctype>
#include <fstream>
#include <limits>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cassert>
using namespace std;
const double range=0.001;//误差
struct node//点
{
    double x,y;
    friend bool operator < (node nx,node ny)
    {
        return nx.x>ny.x;
    }
};
struct wire//线
{
    node p;
    node q;
} a[200];
int solve(int m,int n);
//map<node,int> ma;
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        //ma.clear();
        for(int i=0; i<n; i++)
        {
            scanf("%lf %lf %lf %lf",&a[i].p.x,&a[i].p.y,&a[i].q.x,&a[i].q.y);
            if(a[i].p.x>a[i].q.x)
                swap(a[i].p,a[i].q);
        }
        int ans=0;
        for(int i=0; i<n-1; i++)
            for(int j=i+1; j<n; j++)
                ans+=solve(i,j);
        printf("%d\n",ans);
    }
    return 0;
}
int solve(int m,int n)
{
    node *p1=&a[m].p,*p2=&a[m].q,*q1=&a[n].p,*q2=&a[n].q;
    if( min(p1->x,p2->x) <= max(q1->x,q2->x)+range &&
        min(q1->x,q2->x) <= max(p1->x,p2->x)+range &&
        min(p1->y,p2->y) <= max(q1->y,q2->y)+range &&
        min(q1->y,q2->y) <= max(p1->y,p2->y)+range
      )//排斥试验
        if(
            ((q1->x-p1->x)*(q1->y-q2->y)-(q1->y-p1->y)*( q1->x-q2->x)) * ((q1->x-p2->x)*(q1->y-q2->y)-(q1->y-p2->y)*(q1->x-q2->x)) <= range &&
            ((p1->x-q1->x)*(p1->y-p2->y)-(p1->y-q1->y)*(p1->x-p2->x)) * ((p1->x-q2->x)*(p1->y-p2->y)-(p1->y-q2->y)*( p1->x-p2->x)) <= range
        )//跨立实验
        {//下面注释的为判断点是否重复
//            double tmpLeft,tmpRight;
//            node t;
//            tmpLeft = (q2->x - q1->x) * (p1->y - p2->y) - (p2->x - p1->x) * (q1->y - q2->y);
//            tmpRight = (p1->y - q1->y) * (p2->x - p1->x) * (q2->x - q1->x) + q1->x * (q2->y - q1->y) * (p2->x - p1->x) - p1->x * (p2->y - p1->y) * (q2->x - q1->x);
//
//            t.x = ((double)tmpRight/(double)tmpLeft);
//
//            tmpLeft = (p1->x - p2->x) * (q2->y - q1->y) - (p2->y - p1->y) * (q1->x - q2->x);
//            tmpRight = p2->y * (p1->x - p2->x) * (q2->y - q1->y) + (q2->x- p2->x) * (q2->y - q1->y) * (p1->y - p2->y) - q2->y * (q1->x - q2->x) * (p2->y - p1->y);
//            t.y = ((double)tmpRight/(double)tmpLeft);
//            t.x=((int)(t.x*10000+0.5))/10000.0;
//            t.y=((int)(t.y*10000+0.5))/10000.0;
//            if(!ma[t])
//            {
//                ma[t]=1;
                return true;
//            }
        }
    return false;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值