You can Solve a Geometry Problem too (判断线段相交)

题目描述

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

分析

本题需要我们判断一共有多少线段相交。
判断两线段是否相交:
kuaika.png
我们分两步确定两条线段是否相交:

  1. 快速排斥试验
    设以线段 P1P2 为对角线的矩形为R, 设以线段 Q1Q2 为对角线的矩形为T,如果R和T不相交,显然两线段不会相交。

  2. 跨立试验
    如果两线段相交,则两线段必然相互跨立对方。若P1P2跨立Q1Q2 ,则矢量 (C - A) 和(D - A)位于矢量(B - A) 的两侧,即 (C - A) × (B - A) * (D - A) × (B - A) < 0。
    上式可改写为: (C - A) × (B - A) * (B - A) × (D - A) > 0。 即交换了第二个叉乘的顺序。
    当 (C - A) × (D - A) = 0 时,说明 (C - A) 和 (D - A)共线,但是因为已经通过快速排斥试验,所以C一定在线段 AB上
    最终,判断CD跨立AB的依据是:(C-A) × (B-A) * (D-A) × (B-A) <=0
    pp.png

C++ 代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define eps 1e-8
const int N = 1e2+10;
using namespace std;
struct node
{
    double x,y;
}point1[N],point2[N];
int judge(struct node &a,struct node &b,struct node &c,struct node &d)
{
	//快速排斥实验
    if(!(max(a.x,b.x)>=min(c.x,d.x) && 
		 max(c.x,d.x)>=min(a.x,b.x) && 
		 max(a.y,b.y)>=min(c.y,d.y) &&
		 max(c.y,d.y)>=min(a.y,b.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);  //CA×BA
    v=(d.x-a.x)*(b.y-a.y)-(b.x-a.x)*(d.y-a.y);  //DA×BA
    w=(a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y);  //AC×DC
    z=(b.x-c.x)*(d.y-c.y)-(d.x-c.x)*(b.y-c.y);  //BC×DC
    return (u*v<=eps && w*z<=eps);  //如果两个乘积都<=eps,则判断两线段相交
}
int main()
{
    int n;
    while(cin>>n,n)
    {
        int ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf%lf%lf",&point1[i].x,&point1[i].y,&point2[i].x,&point2[i].y);
        }
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                if(judge(point1[i],point2[i],point1[j],point2[j]))
                    ans++;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jay_fearless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值