zoukankan      html  css  js  c++  java
  • hdu 1086:You 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): 6425    Accepted Submission(s): 3099


    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
     
    Recommend
    We have carefully selected several similar problems for you:  1392 2108 2150 1348 1147 
     
      计算几何:判断两线段是否相交
      很简单的一道题,套上模板之后直接遍历判断每对线段是否相交,相交就计数,最后输出计数就是交点数。这种题的思路就是做两个验证,这两个验证学名叫快速排斥实验和跨立实验,分别有4个判断和2个判断,只有这两个实验都通过才能说这两条线段相交。详见:
      
     判断两线段是否相交模板: 
     1 struct Point{
     2     double x,y;
     3 };
     4 struct Line{
     5     Point p1,p2;
     6 };
     7 double Max(double a,double b)
     8 {
     9     return a>b?a:b;
    10 }
    11 double Min(double a,double b)
    12 {
    13     return a<b?a:b;
    14 }
    15 double xmulti(Point p1,Point p2,Point p0)
    16 {
    17     return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
    18 }
    19 bool inter(Line l1,Line l2)
    20 {
    21     if( Min(l2.p1.x,l2.p2.x)<=Max(l1.p1.x,l1.p2.x) &&
    22         Min(l2.p1.y,l2.p2.y)<=Max(l1.p1.y,l1.p2.y) &&
    23         Min(l1.p1.x,l1.p2.x)<=Max(l2.p1.x,l2.p2.x) &&
    24         Min(l1.p1.y,l1.p2.y)<=Max(l2.p1.y,l2.p2.y) &&
    25         xmulti(l1.p1,l2.p2,l2.p1)*xmulti(l1.p2,l2.p2,l2.p1)<=0 && 
    26         xmulti(l2.p1,l1.p2,l1.p1)*xmulti(l2.p2,l1.p2,l1.p1)<=0 )
    27         return true;
    28     else 
    29         return false;
    30 }
     本题代码:
     1 #include <iostream>
     2 using namespace std;
     3 struct Point{
     4     double x,y;
     5 };
     6 struct Line{
     7     Point p1,p2;
     8 };
     9 double Max(double a,double b)
    10 {
    11     return a>b?a:b;
    12 }
    13 double Min(double a,double b)
    14 {
    15     return a<b?a:b;
    16 }
    17 double xmulti(Point p1,Point p2,Point p0)
    18 {
    19     return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
    20 }
    21 bool inter(Line l1,Line l2)
    22 {
    23     if( Min(l2.p1.x,l2.p2.x)<=Max(l1.p1.x,l1.p2.x) &&
    24         Min(l2.p1.y,l2.p2.y)<=Max(l1.p1.y,l1.p2.y) &&
    25         Min(l1.p1.x,l1.p2.x)<=Max(l2.p1.x,l2.p2.x) &&
    26         Min(l1.p1.y,l1.p2.y)<=Max(l2.p1.y,l2.p2.y) &&
    27         xmulti(l1.p1,l2.p2,l2.p1)*xmulti(l1.p2,l2.p2,l2.p1)<=0 && 
    28         xmulti(l2.p1,l1.p2,l1.p1)*xmulti(l2.p2,l1.p2,l1.p1)<=0 )
    29         return true;
    30     else 
    31         return false;
    32 }
    33 int main()
    34 {
    35     int N;
    36     Line l[105];
    37     while(cin>>N){
    38         if(N==0) break;
    39         for(int i=1;i<=N;i++)
    40             cin>>l[i].p1.x>>l[i].p1.y>>l[i].p2.x>>l[i].p2.y;
    41         int c = 0;
    42         for(int i=1;i<=N-1;i++)
    43             for(int j=i+1;j<=N;j++)
    44                 if(inter(l[i],l[j]))
    45                     c++;
    46         cout<<c<<endl;
    47     }
    48     return 0;
    49 }

    Freecode : www.cnblogs.com/yym2013

  • 相关阅读:
    BDB
    nginx配置文件祥解
    区分虚方法,抽象方法如此简单
    nginx+tomcat集群负载均衡(实现session复制)
    nginx配置conf
    高手博客 网址收藏
    IE6,7,8下使用Javascript记录光标选中范围(已补全)(已解决单个节点内部重复字符的问题)
    (造福中国前端界)纯前端Js完美解决各种汉字urlencode,urldecode,编码解码问题,不借助任何字库,内码表,轻松一行,兼容IE8+,chrome,firefox,safari等主流浏览器
    共享两个最近手机项目的HTML5的widget,星星评级以及标签选项卡,样式美观,iphone/android可用
    共享下改进后的combox,页面原型参考dojo的,比网上jQuery的那些combox功能强,代码更小
  • 原文地址:https://www.cnblogs.com/yym2013/p/3539371.html
Copyright © 2011-2022 走看看