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): 9596    Accepted Submission(s): 4725


    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
    给出N个线段,求线段相交的个数
     1 #include <iostream>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <cstdio>
     5 using namespace std;
     6 const int Max = 110;
     7 const double eps = 0.000001;
     8 struct Point
     9 {
    10     double x, y;
    11     Point(double x = 0, double y = 0) : x(x), y(y) {}
    12 };
    13 struct Line
    14 {
    15     Point start, End;
    16 };
    17 Line line[Max];
    18 typedef Point Vector;
    19 Vector operator- (Vector A, Vector B)
    20 {
    21     return Vector(A.x - B.x, A.y - B.y);
    22 }
    23 double Cross(Vector A, Vector B)
    24 {
    25     return A.x * B.y - A.y * B.x;
    26 }
    27 bool OnSegment(Point A, Point B, Point C)
    28 {
    29     double MinX, MaxX, MinY, MaxY;
    30     if (A.x - B.x > eps)
    31     {
    32         MinX = B.x;
    33         MaxX = A.x;
    34     }
    35     else
    36     {
    37         MinX = A.x;
    38         MaxX = B.x;
    39     }
    40     if (A.y - B.y > eps)
    41     {
    42         MinY = B.y;
    43         MaxY = A.y;
    44     }
    45     else
    46     {
    47         MinY = A.y;
    48         MaxY = B.y;
    49     }
    50     // 大于等于 >=  -eps
    51     if (C.x - MinX >= -eps && MaxX - C.x >= -eps && C.y - MinY >= -eps && MaxY - C.y >= -eps)
    52         return true;
    53     return false;
    54 }
    55 bool solve(Line A, Line B)
    56 {
    57     double c1 = Cross(A.End - A.start, B.start - A.start);
    58     double c2 = Cross(A.End - A.start, B.End - A.start);
    59     double c3 = Cross(B.End - B.start, A.start - B.start);
    60     double c4 = Cross(B.End - B.start, A.End - B.start);
    61     if (c1 * c2 < 0 && c3 * c4 < 0)  // && 手残写成了 || wa了好几次
    62         return true;
    63     if (c1 == 0 && OnSegment(A.start, A.End, B.start))
    64         return true;
    65     if (c2 == 0 && OnSegment(A.start, A.End, B.End))
    66         return true;
    67     if (c3 == 0 && OnSegment(B.start, B.End, A.start))
    68         return true;
    69     if (c4 == 0 && OnSegment(B.start, B.End, A.End))
    70         return true;
    71     return false;
    72 }
    73 
    74 int main()
    75 {
    76     int n;
    77     while (scanf("%d", &n) != EOF && n)
    78     {
    79         int res = 0;
    80         for (int i = 1; i <= n; i++)
    81         {
    82             scanf("%lf%lf%lf%lf", &line[i].start.x, &line[i].start.y, &line[i].End.x, &line[i].End.y);
    83         }
    84         for (int i = 1; i <= n; i++)
    85         {
    86             for (int j = i + 1; j <= n; j++)
    87             {
    88                 if (solve(line[i], line[j]))
    89                     res++;
    90             }
    91         }
    92         printf("%d
    ", res);
    93     }
    94     return 0;
    95 }
    View Code
     
     
  • 相关阅读:
    c++小游戏——2048
    c++小游戏——贪吃蛇
    218 事件处理 on() 绑定事件
    217 jQuery 元素操作:遍历,创建,添加(append、prepend、after、before),删除(remove、empty、html)
    216 jQuery 文本属性值:html() 、text() 、 val()
    215 jQuery 属性操作:prop() 、 attr() 、 data()
    214 jQuery案例:王者荣耀手风琴效果
    213 jQuery 事件切换:hover()
    212 停止动画排队: stop()
    211 jQuery 自定义动画:animate()
  • 原文地址:https://www.cnblogs.com/zhaopAC/p/5479247.html
Copyright © 2011-2022 走看看