zoukankan      html  css  js  c++  java
  • HDU1086判断线段相交

    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): 10924    Accepted Submission(s): 5393


    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
     
      可以把线段看成直线,计算出交点,看交点是不是在可行的范围内;注意一下斜率等于0的情况就行了;
     
     1 #include<algorithm>
     2 #include<iostream>
     3 #include<iomanip>
     4 #include<cstdlib>
     5 #include<cstring>
     6 #include<vector>
     7 #include<cstdio>
     8 #include<stack>
     9 #include<queue>
    10 #include<cmath>
    11 #include<ctime>
    12 #include<set>
    13 #include<map>
    14 #define ll long long
    15 #define rep(i,a,b) for(register int i=a;i<=b;i++)
    16 #define inf 1<<29
    17 #define re register
    18 using namespace std;
    19 #define eps 1e-6
    20 const int N=110;
    21 struct point{
    22     double x,y;
    23 }d[2];
    24 struct line{
    25     double k,b;
    26     double l,r,ly,ry;
    27 }L[N];
    28 int n;
    29 inline bool pd(double x,double y,int k) {
    30     return (x>=L[k].l&&x<=L[k].r&&y>=L[k].ly&&y<=L[k].ry);
    31 }
    32 inline bool judge(int i,int j) {
    33     if(fabs(L[i].k-L[j].k)<=eps) return 0;
    34     if(fabs(L[i].k-0)<=eps&&fabs(L[j].k-0)<=eps) return 0;
    35     if(fabs(L[i].k-0)>eps&&fabs(L[j].k-0)>eps) {
    36         double x=(L[j].b-L[i].b)/(L[i].k-L[j].k),
    37             y=L[i].k*x+L[i].b;
    38         if(pd(x,y,i)&&pd(x,y,j)) return 1;
    39         else return 0;
    40     }
    41     if(fabs(L[i].k-0)<=eps) {
    42         double x=L[i].l;
    43         double y=L[j].k*x+L[j].b;
    44         return (y>=L[j].ly&&y<=L[j].ry);
    45     }
    46     double x=L[j].l;
    47     double y=L[i].k*x+L[i].b;
    48     return (y>=L[i].ly&&y<=L[i].ry);
    49 }
    50 int main() {
    51     freopen("Y.in","r",stdin);
    52     freopen("Y.out","w",stdout);
    53     while(scanf("%d",&n)&&n) {
    54         for(int i=1;i<=n;i++) {
    55             scanf("%lf%lf%lf%lf",&d[0].x,&d[0].y,&d[1].x,&d[1].y);
    56             L[i].l=min(d[0].x,d[1].x),L[i].r=max(d[0].x,d[1].x);
    57             L[i].ly=min(d[0].y,d[1].y),L[i].ry=max(d[0].y,d[1].y);
    58             if(fabs(d[1].x-d[0].x)<=eps)
    59                 L[i].k=0;
    60             else {
    61                 L[i].k=(d[1].y-d[0].y)/(d[1].x-d[0].x);
    62                 L[i].b=d[0].y-L[i].k*d[0].x;
    63             }
    64         }
    65         int ans=0;
    66         for(int i=1;i<n;i++)
    67             for(int j=i+1;j<=n;j++)
    68                 if(judge(i,j)) ans++;
    69         cout<<ans<<endl;
    70     }
    71     return 0;
    72 }
  • 相关阅读:
    基于遗传算法(Genetic Algorithm)的TSP问题求解(C)
    分治思想:合并排序和快速排序
    冒泡排序和选择排序
    WPF线程
    DataGrid属性和事件
    WPF限制TextBox只能输入数字
    键盘键值对应表
    转换人民币大小金额
    查找DataGrid某个单元格中的控件
    WPF中DataGrid使用初步
  • 原文地址:https://www.cnblogs.com/ypz999/p/7107127.html
Copyright © 2011-2022 走看看