zoukankan      html  css  js  c++  java
  • You can Solve a Geometry Problem too(线段求交)

    http://acm.hdu.edu.cn/showproblem.php?pid=1086

    You can Solve a Geometry Problem too

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


    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
     
    题解:本题题干已经排除了两线重合的多边交于一点的情况,故直接枚举所有的边是否相交即可
     1 #include<cstdio>
     2 #include<cmath>
     3 using namespace std;
     4 #define eps 1e-6
     5 #define N 105
     6 struct point{
     7     double x , y ;
     8     point(double x_, double y_){
     9          x = x_;
    10          y = y_;
    11     }
    12     point(){}
    13     point operator - (const point a) const
    14     {
    15         return point(x-a.x,y-a.y);
    16     }
    17     double operator * (const point a) const
    18     {
    19         return x*a.y - a.x*y;
    20     }
    21 };
    22 
    23 struct line{
    24     point s , t;
    25 }L[N];
    26 
    27 int main()
    28 {
    29     int T;
    30     while(~scanf("%d",&T),T)
    31     {
    32         for(int i = 0 ;i < T ; i++)
    33         {
    34             scanf("%lf%lf%lf%lf",&L[i].s.x,&L[i].s.y,&L[i].t.x,&L[i].t.y);
    35         }
    36         int ans = 0;
    37         for(int i = 0 ; i < T ; i++)
    38         {
    39             for(int j = i+1 ; j < T ; j++)//j从i开始保证不会重复判断
    40             {
    41                // if(i==j) continue;
    42                 point A = L[i].s;
    43                 point B = L[i].t;
    44                 point C = L[j].s;
    45                 point D = L[j].t;
    46                 if((((D-C)*(A-C))*((D-C)*(B-C)))>eps) {continue;}
    47                 if((((D-A)*(B-A))*((C-A)*(B-A)))>eps) {continue;}
    48                     ans++;
    49             }
    50         }
    51         printf("%d
    ",ans);
    52     }
    53     return 0;
    54 }

    也可以把他们写成函数在外面

     1 #include <cstdio>
     2 #include <cmath>
     3 using namespace std;
     4 #define eps 1e-8
     5 #define N 105
     6 struct point{
     7     double x, y;
     8     point(){}
     9     point(double _x, double _y) {
    10         x = _x, y = _y;
    11     }
    12     
    13     point operator - (point a){
    14         return point(x-a.x, y-a.y);
    15     }
    16     
    17     double operator * (point a){
    18         return x*a.y - y*a.x;
    19     }
    20 };
    21 
    22 struct line{
    23     point s, t;
    24 }L[N];
    25 
    26 bool ck(line a, line b)
    27 {
    28     point A = a.s, B = a.t, C = b.s, D = b.t;
    29     if(((C-A)*(B-A)) *((D-A)*(B-A)) > eps) return false;
    30     if(((A-C)*(D-C)) *((B-C)*(D-C)) > eps) return false;
    31     return true;
    32 }
    33 
    34 int main()
    35 {
    36     int n;
    37     while(~scanf("%d", &n), n)
    38     {
    39         for(int i = 0; i < n; i++)
    40             scanf("%lf %lf %lf %lf", &L[i].s.x, &L[i].s.y, &L[i].t.x, &L[i].t.y);
    41         int cnt = 0;
    42         for(int i = 0; i < n; i++)
    43             for(int j = i+1; j < n; j++)
    44                 cnt += ck(L[i], L[j]);
    45         printf("%d
    ", cnt);
    46     }
    47 }
  • 相关阅读:
    mybatis批量更新报错
    Axure8破解码
    小程序开发-Step1
    2018新年计划
    java 写 Excel(不生成实体文件,写为流的形式)
    git 生成公钥 使用命令行无需输入用户名密码(windows)
    Node.js:上传文件,服务端如何获取文件上传进度
    Express:模板引擎深入研究
    windows下nginx的安装及使用
    Chrome开发者工具详解-Network面板
  • 原文地址:https://www.cnblogs.com/shanyr/p/4687256.html
Copyright © 2011-2022 走看看