You can Solve a Geometry Problem too(计算几何_求线段相交)

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
题意:给出n组线段,求直线两两相交的点的个数;
思路:要求直线两两相交,分为两步来做:
(1)快速排斥实验
设以线段 P1P2 为对角线的矩形为R, 设以线段 Q1Q2 为对角线的矩形为T,如果R和T不相交,显然两线段不会相交。
(2)跨立实验
如果两线段相交,则两线段必然相互跨立对方。若P1P2跨立Q1Q2 ,则矢量 ( P1 - Q1 ) 和( P2 - Q1 )位于矢量( Q2 - Q1 ) 的两侧,即( P1 - Q1 ) × ( Q2 - Q1 ) * ( P2 - Q1 ) × ( Q2 - Q1 ) < 0。上式可改写成( P1 - Q1 ) × ( Q2 - Q1 ) * ( Q2 - Q1 ) × ( P2 - Q1 ) > 0。当 ( P1 - Q1 ) × ( Q2 - Q1 ) = 0 时,说明 ( P1 - Q1 ) 和 ( Q2 - Q1 )共线,但是因为已经通过快速排斥试验,所以 P1 一定在线段 Q1Q2上;同理,( Q2 - Q1 ) ×(P2 - Q1 ) = 0 说明 P2 一定在线段 Q1Q2上。所以判断P1P2跨立Q1Q2的依据是:( P1 - Q1 ) × ( Q2 - Q1 ) * ( Q2 - Q1 ) × ( P2 - Q1 ) >= 0。同理判断Q1Q2跨立P1P2的依据是:( Q1 - P1 ) × ( P2 - P1 ) * ( P2 - P1 ) × ( Q2 - P1 ) >= 0。具体情况如下图所示:
ps:看的这篇文章明白的大体思路: http://dev.gameres.com/Program/Abstract/Geometry.htm#判断两线段是否相交
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
struct point
{
    double x,y;
};//某个点的x,y点坐标;
struct line
{
    point p1,p2;//代表线上的两个点;
}a[110];
double chacheng (struct point p1,struct point p2,point p3)
{
    //叉乘的概念 (a,b)×(c,d)=a*d-b*c;切记
    return (p1.x-p3.x)*(p2.y-p3.y)-(p1.y-p3.y)*(p2.x-p3.x);
}//叉乘的方向是垂直于平面的 ,用右手螺旋法则 a×b=-b×a;
int judge(struct line l1,struct line l2)
{
    //这是求直线相交的中心思想
    if( min(l2.p1.x,l2.p2.x)<=max(l1.p1.x,l1.p2.x)&&min(l2.p1.y,l2.p2.y)<=max(l1.p1.y,l1.p2.y)&&min(l1.p1.x,l1.p2.x)<=max(l2.p1.x,l2.p2.x) &&min(l1.p1.y,l1.p2.y)<=max(l2.p1.y,l2.p2.y)
       &&chacheng(l1.p1,l2.p2,l2.p1)*chacheng(l1.p2,l2.p2,l2.p1)<=0 &&chacheng(l2.p1,l1.p2,l1.p1)*chacheng(l2.p2,l1.p2,l1.p1)<=0 )
        return 1;
    else
        return 0;
}
int main()
{
    int n,i,j;
    int sum=0;
    while(~scanf("%d",&n))
        {
        if(n==0)
        break;
       sum=0;
        for(i=0;i<n;i++)
            scanf("%lf %lf %lf %lf",&a[i].p1.x,&a[i].p1.y,&a[i].p2.x,&a[i].p2.y);
        for(i=0;i<n-1;i++)
            for(j=i+1;j<n;j++)
                if(judge(a[i],a[j]))
                    sum++;
        printf("%d\n",sum);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Rocky0429

一块也是爱

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

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

打赏作者

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

抵扣说明:

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

余额充值