zoukankan      html  css  js  c++  java
  • HDU1086You can Solve a Geometry Problem too (斜率问题)

    You can Solve a Geometry Problem too

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


    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
     

    Author
    lcy
     
    思路:排除两线段没有相交可能的情况,即线段的最大点比另一条线段的最小点还小
    然后两线段相交要求   线段1斜率介于线段1的一端点与另一条线段2的俩端点的斜率之间
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    struct node
    {
        double x;
        double y;
    };
    double chaji(node a,node b,node c)    //两条线段斜率的差,double !!!
    {
        return (b.y-a.y)*(c.x-a.x)-(c.y-a.y)*(b.x-a.x);
    }
    int count(node a1,node a2,node b1,node b2)
    {
        if(max(a1.x,a2.x)<min(b1.x,b2.x))
            return 0;
        if(max(a1.y,a2.y)<min(b1.y,b2.y))
          #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    struct node
    {
        double x;
        double y;
    };
    double chaji(node a,node b,node c)    //两条线段斜率的差,double !!!
    {
        return (b.y-a.y)*(c.x-a.x)-(c.y-a.y)*(b.x-a.x);
    }
    int count(node a1,node a2,node b1,node b2)
    {
        if(max(a1.x,a2.x)<min(b1.x,b2.x))
            return 0;
        if(max(a1.y,a2.y)<min(b1.y,b2.y))
            return 0;
        if(min(a1.x,a2.x)>max(b1.x,b2.x))
            return 0;
        if(min(a1.y,a2.y)>max(b1.y,b2.y))
            return 0;
     
          if(chaji(a1,a2,b1)*chaji(a1,b2,a2)<0)
          return 0;
          if(chaji(b1,b2,a1)*chaji(b1,a2,b2)<0)
          return 0;
        return 1;
    }
    int main()
    {
        int n,t;
        node nodea[105],nodeb[105];
        while(~scanf("%d",&n))
        {
            if(n==0)
                break;
            t=0;
            for(int i=1; i<=n; i++)
            {
                scanf("%lf%lf%lf%lf",&nodea[i].x,&nodea[i].y,&nodeb[i].x,&nodeb[i].y);
            }
            for(int i=1; i<n; i++)
            {
                for(int j=i+1; j<=n; j++)
                {
                   // printf("!!!!
    ");
                    if(count(nodea[i],nodeb[i],nodea[j],nodeb[j]))
                    {
                      //  printf("@222
    ");
                        t++;
                    }
     
                }
            }
            printf("%d
    ",t);
        }
     
        return 0;
    }
  • 相关阅读:
    分享5个viewport相关的jQuery插件
    超棒的响应式jQuery网格布局插件 grida licious
    6款不容错过的超棒倒计时jQuery插件
    分享45套2011年和2012年的高质量免费网站模板
    分享11个使用方便的免费智能手机UI套件
    推荐30款超精致的体育类型的网站设计
    HDOJ1001
    HDOJ1003
    HDOJ1000
    HDOJ1002
  • 原文地址:https://www.cnblogs.com/dshn/p/4750701.html
Copyright © 2011-2022 走看看