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.0
    Citrix 服务器虚拟化之十 Xenserver高可用性HA
    JS实现——俄罗斯方块
    一种文件捆绑型病毒研究
    XP系统登录界面,需要手动点击用户帐户后才会出现输入密码的界面
    加密javascript代码
    Python的在线编辑环境
  • 原文地址:https://www.cnblogs.com/dshn/p/4750701.html
Copyright © 2011-2022 走看看