专题:计算几何学 线段相交 hdu1086

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086
代码链接:
https://github.com/fangtanchen/Learning/tree/master/1_Algorithm/hdu/ACM_STEP/ch7/s1/2_segment_intersection

题目

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

思路

  • 简单题目
    • 这题的数据严谨,过不了不必担忧
    • 利用A线段如果与B线段相交,A线段2端点在B线段两边,B线段2端点在A线段两边的性质求解
    • 利用向量之间的叉积

代码如下

#include<iostream>
#include<cstdio>
#include<cstring>
//#include<vector>
#include<algorithm>
#include<cmath>

#ifdef L_JUDGE
#pragma warning(disable:4996)
#endif

using namespace std;
const int MAXN=110;

struct Node{
    double x,y;
    Node(double a=0,double b=0){
        x=a;y=b;
    }
};

Node pt[MAXN][2];
int N;

double Direction(const Node &p1,const Node &p2,const Node &p3){
    return (p3.x-p1.x)*(p2.y-p1.y)-(p2.x-p1.x)*(p3.y-p1.y);
}
bool OnSegment(const Node &p1,const Node &p2,const Node &p3){
    double maxx=max(p1.x,p2.x);
    double minx=min(p1.x,p2.x);
    double maxy=max(p1.y,p2.y);
    double miny=min(p1.y,p2.y);
    return (p3.x>=minx)&&(p3.x<=maxx)
        &&(p3.y<=maxy)&&(p3.y>=miny);
}

/*
bool OnSegment(const Node &p1,const Node &p2,const Node &p3){
    double maxx=max(p1.x,p2.x);
    double minx=min(p1.x,p2.x);
    double maxy=max(p1.y,p2.y);
    double miny=min(p1.y,p2.y);
    return (p3.x>minx)&&(p3.x<maxx)
        &&(p3.y<maxy)&&(p3.y>miny);
}*/


int SegIntersect(const Node &p1,const Node &p2,
        const Node &p3,const Node &p4){
    double d1=Direction(p1,p2,p3);
    double d2=Direction(p1,p2,p4);
    double d3=Direction(p3,p4,p1);
    double d4=Direction(p3,p4,p2);
    if((d1*d2<=0)&&(d3*d4<=0))return 1;
    return 0;
}


int main(){
    #ifdef L_JUDGE
        freopen("in.txt","r",stdin);
//      freopen("out.txt","w",stdout);
    #endif
        while(scanf("%d",&N),N){
            int ans=0;
            for(int i=0;i<N;i++){
                scanf("%lf%lf%lf%lf",&pt[i][0].x,&pt[i][0].y,&pt[i][1].x,&pt[i][1].y);
                for(int j=0;j<i;j++){
                    ans+=SegIntersect(pt[i][0],pt[i][1],pt[j][0],pt[j][1]);
                }
            }
            printf("%d\n",ans);
        }

    #ifdef L_JUDGE
        fclose(stdin);
        fclose(stdout);
        system("out.txt");
    #endif

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值