You can Solve a Geometry Problem too----判断两线段是否相交

快速排斥实验+跨立实验

先来讲解一下题目需要用到的前置技能
1)快速排斥实验
2)跨立实验

先来讲快速排斥吧
快速排斥可以理解为两条线段相交的“预处理”,先大体的判断一下,如果最开始最简单的判断都过不了那么两条线段一定不相交,快速排斥实验,就是以两条线段为对角线,建立两个矩形,如果两个矩形不相交,那么两条线段一定不相交,如果两个矩形相交了,还需要经过跨立实验来判断两条线段是不是相交。(脑补一下画面,快速排斥是不是很简单。。!!)


从网上盗了张图,是不是简单明了!

然后是跨立实验,其实跨立实验的理论也挺简单,就是要求了一点数学的知识,叉积(不知道叉积的说明你的高数完蛋了。。自己去百度),其实跨立实验一句话就能够把原理解释明白。如果两条线段相交,那么两条线段必然跨立对方。若A1A2跨立B1B2,则矢量(A1-B1)和(A2-B1)位于矢量(B2-B1)的两侧。即(A1-B1) × (B2-B1) * (A2-B1) × (B2-B1)<0。 上式可改写成(A1-B1) × (B2-B1) * (B2-B1) × (A2-A1)>0。 应该判断两次,即两条线段都要为直线,判断另一直线的两端点是否在它两边,若是则两线段相交。

若积极满跨立实验是不行的,如下面的情况:

即两条线段在同一条直线上。所以我们要同时满足两次跨立和快速排斥实验。

判断A1A2跨立B1B2的依据是:(A1-B1) × (B2-B1) * (B2-B1) × (A2-B1) >= 0。

判断B1B2跨立A1A2的依据是:(B1-A1) × (A2-A1) * (A2-A1) × (B2-A1) >= 0。

在此,我一定要把我柴爷的跨立实验的代码贴在这,大神就是大神,代码就是简洁

int opposite_side(Point p1, Point p2, Line l){
    return xmult(l.a, p1, l.b)*xmult(l.a, p2, l.b) < -eps;
}


int intersect_ex(Line u, Line v){
return opposite_side(u.a, u.b, v) 
&& opposite_side(v.a, v.b, u);
} 


膜拜中。。。。

上面写了这么一大堆,但是我从网上看见了一张神图,简洁明了的表达了我上面写的一大坨。。。


不多不说,我要讲的都在这张图里了,什么也不说了,上题!

You can Solve a Geometry Problem too

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9676    Accepted Submission(s): 4771


Problem 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 0
 

Sample Output
   
   
1 3

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086

这个题是让你求交点的个数,注意,不用去管交点是否重叠(我一开始想多了),简单粗暴的两线段相交,直接上代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define eps 0.00000001
using namespace std;
struct node
{
    double x,y;
} point1[1000],point2[1000];
int kuaikua(struct node &a,struct node &b,struct node &c,struct node &d)
{
    //函数名比较暴力。。。。
    if(!(min(a.x,b.x)<=max(c.x,d.x) && min(c.y,d.y)<=max(a.y,b.y)&&
        min(c.x,d.x)<=max(a.x,b.x) && min(a.y,b.y)<=max(c.y,d.y)))
    //快速排斥实验
    return 0;
    double u,v,w,z;//分别记录两个向量
    u=(c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
    v=(d.x-a.x)*(b.y-a.y)-(b.x-a.x)*(d.y-a.y);
    w=(a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y);
    z=(b.x-c.x)*(d.y-c.y)-(d.x-c.x)*(b.y-c.y);
    return (u*v<=eps && w*z<=eps);
}
int main()
{
    int n,i;
    int j;
    while(cin>>n,n)
    {
        int ans=0;
        for(i=0; i<n; i++)
        {
            scanf("%lf%lf%lf%lf",&point1[i].x,&point1[i].y,&point2[i].x,&point2[i].y);
        }
        for(i=0; i<n; i++)
        {
            for(j=i+1; j<n; j++)
            {
                if(kuaikua(point1[i],point2[i],point1[j],point2[j]))
                    ans++;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


这是个计算几何的题,我好方。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值