zoukankan      html  css  js  c++  java
  • 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;
    }

     

     

  • 相关阅读:
    python爬虫统计上证指数周、月涨跌现象
    python每日一题:采用正则表达式,beautifulsoap,xpath爬取网站数据
    谈股市与月份的关系
    python之正则表达式
    python每日一题:使用代理服务器爬虫
    python之cookie使用
    python每日一题:爬虫入门之利用xpath查找网页元素节点
    python每日一题:制作网页,与女朋友的点点滴滴
    【Java基础】Java11 新特性
    【Java基础】Java10 新特性
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5427298.html
Copyright © 2011-2022 走看看