"Template" segments intersect
1#include <stdio.h>2#include <iostream>3#include <math.h>4#include <algorithm>5 using namespacestd;6 struct Point7 {8 Doublex, y;9 };TenPoint a[ the][2];//A[i][0] Represents the head of the section I segment, a[i][1] represents the tail One DoubleFanDoubleXDoubley) A { - returnX>y?x:y; - } the - DoubleFinDoubleCDoubled) - { - returnC<d?c:d; + } - + DoubleCNT (point A,point B) A { at returnsqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (a.y-b.y)); - } - - int is(Point a,point b,point c,point D) - { - if(a.x==b.x&&c.x==d.x) in { - return 0; to } + if(a.x==b.x&&c.x!=d.x) - { the Doublem1=a.x; * DoubleM2= (a.x-c.x) * (D.Y-C.Y)/(d.x-c.x) +c.y; $ if(M1<=fan (a.x,b.x) &&m1>=fin (a.x,b.x) &&m2>=fin (A.Y,B.Y) &&m2<=fan (A.Y,B.Y) &&m1<=fan (c.x,d.x) &&m1>=fin (c.x,d.x) &&m2>=fin (C.Y,D.Y) &&m2<=Fan (C.Y,D.Y))Panax Notoginseng return 1; - } the if(c.x==d.x&&a.x!=b.x) + { A Doublem1=c.x; the Doublem2=a.y+ (B.Y-A.Y) * (c.x-a.x)/(b.x-a.x); + if(M1<=fan (a.x,b.x) &&m1>=fin (a.x,b.x) &&m2>=fin (A.Y,B.Y) &&m2<=fan (A.Y,B.Y) &&m1<=fan (c.x,d.x) &&m1>=fin (c.x,d.x) &&m2>=fin (C.Y,D.Y) &&m2<=Fan (C.Y,D.Y)) - return 1; $ } $ Doublek1= (B.Y-A.Y)/(b.x-a.x); - DoubleK2= (D.Y-C.Y)/(d.x-c.x); - Doublem1,m2,x,y; the if(K1==K2)return 0; - ElseWuyi { theM1=a.y-k1*a.x; -m2=c.y-k2*c.x; Wux= (m1-m2)/(k2-K1); -y=k1*x+M1; About if(X<=fan (a.x,b.x) &&x>=fin (a.x,b.x) &&y>=fin (A.Y,B.Y) &&y<=fan (A.Y,B.Y) & &x<=fan (c.x,d.x) &&x>=fin (c.x,d.x) &&y>=fin (C.Y,D.Y) &&y<=Fan (C.Y,D.Y)) $ return 1; - } - return 0; - } A + intMain () the { - intCAS =1; $ intn,i,j; the while(~SCANF ("%d",&N), N) the { the intCNT =0; the for(i =0; i<n;i++) -scanf"%LF%LF%LF%LF", &a[i][0].x,&a[i][0].y,&a[i][1].x,&a[i][1].Y);//the end and end coordinates of a segment in for(i =0; i<n;i++) the { the for(j = i+1; j<n;j++) About { the if( is(a[i][0],a[i][1],a[j][0],a[j][1])) thecnt++; the } + } -printf"%d\n", CNT); the }Bayi return 0; the}
Template segment Intersection