http://acm.hdu.edu.cn/showproblem.php?pid=1086You 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 Descriptionmany Geometry (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 was always much trouble, but this problem was very easy, afte R all we is now attending an exam, not a contest:)
Give you N (1<=n<=100) segments (line segment), please output the number of all intersections (intersection). You should count repeatedly if M (m>2) segments intersect on the same point.
Note:
You can assume that, segments would not intersect at the more than one point.
Inputinput contains multiple test cases. Each test case is 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 is coordinates of the segment ' s ending.
A test case, starting with 0 terminates, the input and this test are not processed.
Outputfor, print the number of intersections, and one line one case.
Sample Input20.00 0.00 1.00 1.000.00 1.00 1.00 0.0030.00 0.00 1.00 1.000.00 1.00 1.00 0.0000.00 0.00 1.00 0.000 The problem is that the subject has been drained In addition to the overlapping of the two lines of the multilateral intersection of the situation, so directly enumerate all the edges intersect can
1#include <cstdio>2#include <cmath>3 using namespacestd;4 #defineEPS 1e-65 #defineN 1056 structpoint{7 Doublex, y;8PointDoubleX_Doubley_) {9x =x_;Teny =Y_; One } A Point () {} -Pointoperator- (ConstPoint a)Const - { the returnPoint (x-a.x,y-a.y); - } - Double operator* (ConstPoint a)Const - { + returnx*a.y-a.x*y; - } + }; A at structline{ - Point S, t; - }l[n]; - - intMain () - { in intT; - while(~SCANF ("%d",&t), T) to { + for(inti =0; i < T; i++) - { thescanf"%LF%LF%LF%LF",&l[i].s.x,&l[i].s.y,&l[i].t.x,&l[i].t.y); * } $ intAns =0;Panax Notoginseng for(inti =0; i < T; i++) - { the for(intj = i+1; J < T; J + +)//J Starting from I, I promise I won't repeat judgment . + { A //if (i==j) continue; thePoint A =L[i].s; +Point B =l[i].t; -Point C =L[j].s; $Point D =l[j].t; $ if(((((d-c) * (A-C)) * ((((d-c) * (B-C))) (>eps) {Continue;} - if(((((d-a) * (B-A)) * ((((c-a) * (B-A))) (>eps) {Continue;} -ans++; the } - }Wuyiprintf"%d\n", ans); the } - return 0; Wu}
Or they can be written as functions outside.
1#include <cstdio>2#include <cmath>3 using namespacestd;4 #defineEPS 1e-85 #defineN 1056 structpoint{7 Doublex, y;8 Point () {}9PointDouble_x,Double_y) {Tenx = _x, y =_y; One } A -Pointoperator-(Point a) { - returnPoint (x-a.x, ya.y); the } - - Double operator*(Point a) { - returnx*a.y-y*a.x; + } - }; + A structline{ at Point S, t; - }l[n]; - - BOOLck (Line A, line B) - { -Point A = a.s, B = a.t, C = B.S, D =b.t; in if(((c-a) * (B-A)) * ((d-a) * (b-a)) > EPS)return false; - if(((a-c) * (D-C)) * ((B-C) * (D-C)) > EPS)return false; to return true; + } - the intMain () * { $ intN;Panax Notoginseng while(~SCANF ("%d", &N), N) - { the for(inti =0; I < n; i++) +scanf"%lf%lf%lf%lf", &l[i].s.x, &l[i].s.y, &l[i].t.x, &l[i].t.y); A intCNT =0; the for(inti =0; I < n; i++) + for(intj = i+1; J < N; J + +) -CNT + =ck (L[i], l[j]); $printf"%d\n", CNT); $ } -}
You can Solve a Geometry problem too (segment intersection)