Title: Give a convex polygon (the point is not given in order), and then calculate the length of the given segment in the convex polygon, if the boundary is not calculated.
Analysis: WA2. WA3 ... WA4. WA11 ... WA has nothing to say, the details must be considered clearly, coincide with the time must be 0
The code is as follows:
=========================================================================================================
#include <stdio.h>#include<math.h>#include<algorithm>#include<iostream>using namespacestd;Const intMAXN =1007;Const DoubleEPS = 1e- -;Const DoubleFarx = 4e4+7;intSign (Doublex) { if(x > EPS)return 1; if(Fabs (x) < EPS)return 0; return-1;}structpoint{Doublex, y; Point (Doublex=0,Doubley=0): X (x), Y (y) {} pointoperator- (ConstPoint &t)Const{ returnPoint (X-t.x, yt.y); } BOOL operator== (ConstPoint &t)Const{ returnSign (x-t.x) = =0&& sign (y-t.y) = =0; } Double operator^(ConstPoint &t)Const{ returnx*t.y-y*T.x; } Double operator*(ConstPoint &t)Const{ returnX*t.x + y*T.y; }};DoubleDist (Point t1, point T2) {returnsqrt ((T1-T2) * (t1-T2));}structsegment{Point S, E; DoubleA, B, C;///ax + by = C;segment (Point S=0, point e=0): s (s), E (e) {a= S.y-e.y; b= e.x-s.x; C= e.x*s.y-s.x*e.y; } intInter (ConstSegment &tmp)Const{ intT1 = sign ((S-E) ^ (TMP. S-E)); intT2 = sign ((S-E) ^ (TMP. E-E)); if(T1 = =0&& T2 = =0) return-1; if(T1*t2 = =-1) return 1; if(ABS (T1+T2) = =1)///If there is a complete intersection or a little intersection, do not consider coincident return 2; return false; } Point Crossnode (ConstSegment &tmp)Const {///intersection of two line segmentsPoint result; Result.x= (c*tmp.b-tmp.c*b)/(a*tmp.b-tmp.a*b); Result.y= (c*tmp.a-tmp.c*a)/(b*tmp.a-tmp.b*a); returnresult; } BOOLONSEG (ConstPoint &p) {///determine if the point is on a line segment if(sign (S-E) ^ (p-e)) = =0) if(sign (p.x-s.x) * (p.x-e.x)) <=0) if(sign (P.Y-S.Y) * (P.Y-E.Y)) <=0) return true; return false; }};structpolygon{intN///there are n pointsPoint VERTEX[MAXN]; intPoint_in_poly (ConstPoint &p) {///dot in the polygon 1, or outside-1, or edge 0segment Ray (p, point (Farx, p.y)); intCNT =0; for(intI=0; i<n; i++) {segment L (Vertex[i], vertex[i+1]); if(L.ONSEG (p))return 0; if(Ray. Onseg (L.S)) {if(L.e.y-l.s.y >EPS) CNT++; } Else if(Ray. Onseg (L.E)) {if(L.s.y-l.e.y >EPS) CNT++; } Else if(L.inter (Ray) = =1&& Ray. Inter (L) = =1) CNT++; } if(CNT%2) return 1; return-1; } DoubleSeg_in_poly (ConstSegment &L1) {///the length of the intersection of the segment and the polygon, first the line segment and the polygon have several intersectionsPoint p[Ten]; intk=0; for(intI=0; i<n; i++) {segment L2 (Vertex[i], vertex[i+1]); if(L1. Inter (L2) = =-1&& L2. Inter (L1) = =-1) return 0; if(k<2&& L1. Inter (L2) &&L2. Inter (L1)) {Point T=L1.crossnode (L2); if(!k | |! (p[0] ==t)) P[k++] =T; } } Doublelen=0; if(k = =2) {///There are two different intersections.Len = Dist (p[0], p[1]); } Else if(k = =1) {///There is an intersection that determines which endpoint is within the polygon if(Point_in_poly (L1. S) = =1) Len= Dist (p[0], L1. S); Else if(Point_in_poly (L1. E) = =1) Len= Dist (p[0], L1. E); } Else if(Point_in_poly (L1. S) = =1) {///when there is no intersection, determine if the segment is within the polygonLen =Dist (L1. S, L1. E); } returnLen; }}; Polygon Poly;BOOLCMP (Point t1, point T2) {returnSign ((t2-poly.vertex[0]) ^ (t1-poly.vertex[0])) <0;}intMain () {intI, M, ki=0; scanf ("%d", &Poly. N); for(i=0; I<poly. N i++) {scanf ("%LF%LF", &poly.vertex[i].x, &poly.vertex[i].y); if(Poly.vertex[ki].y > Poly.vertex[i].y | |(Poly.vertex[ki].y= = Poly.vertex[i].y && poly.vertex[ki].x >poly.vertex[i].x)) Ki=i; } Swap (poly.vertex[0], Poly.vertex[ki]); Sort (Poly.vertex+1, poly.vertex+Poly. N, CMP); Poly.vertex[poly. N]= poly.vertex[0]; scanf ("%d", &M); while(m--) {point A, B; scanf ("%LF%LF%LF%LF", &a.x, &a.y, &b.x, &b.y); Segment L (A, B); printf ("%.6f\n", Poly. Seg_in_poly (L)); } return 0;}/**61 22001 1 4 10**/
INHERITANCE-SGU 129 (length of segment intersecting with polygon)