• hdu1086(线段相交)


    You can Solve a Geometry Problem too

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


    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
    题意:n根直线的交点有多少。
    线段相交转自:此链接
        如果两线段相交,则两线段必然相互跨立对方。若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。具体情况如下图所示:

       

     

    模板题。

    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    
    struct Point{
        double x,y;
    }p[205];
    
    ///叉积
    double mult(Point a, Point b, Point c)
    {
        return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
    }
    
    ///a, b为一条线段两端点c, d为另一条线段的两端点 相交返回true, 不相交返回false
    bool isCross(Point a, Point b, Point c, Point d)
    {
        if (max(a.x,b.x)<min(c.x,d.x))return false;
        if (max(a.y,b.y)<min(c.y,d.y))return false;
        if (max(c.x,d.x)<min(a.x,b.x))return false;
        if (max(c.y,d.y)<min(a.y,b.y))return false;
        if (mult(c, b, a)*mult(b, d, a)<0)return false;
        if (mult(a, d, c)*mult(d, b, c)<0)return false;
        return true;
    }
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF,n){
            int k=1;
            for(int i=1;i<=n;i++){
                scanf("%lf%lf%lf%lf",&p[k].x,&p[k].y,&p[k+1].x,&p[k+1].y);
                k+=2;
            }
            int ans = 0;
            for(int i=1;i<=n;i++){
                for(int j=i+1;j<=n;j++){
                    //printf("%lf %lf %lf %lf
    ",p[2*i-1].x,p[2*i].x,p[2*j-1].x,p[2*j].x);
                    if(isCross(p[2*i-1],p[2*i],p[2*j-1],p[2*j])) ans++;
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }

     

     

  • 相关阅读:
    [CareerCup] 4.6 Find Next Node in a BST 寻找二叉搜索树中下一个节点
    Android 接入支付宝支付实现
    Android 设置软键盘搜索键以及监听搜索键点击事件
    Android 应用监听自身卸载,弹出用户反馈调查
    ndk制作so库,ndk-build不是内部或外部命令。。。的错误
    Error: Your project contains C++ files but it is not using a supported native build system
    Android开发之——依赖冲突Program type already present
    基于Python的开源人脸识别库:离线识别率高达99.38%
    Android5.0以后,materialDesign风格的加阴影和裁剪效果
    Android 5.0 以上监听网络变化
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5427298.html
Copyright © 2020-2023  润新知