Test instructions: Give four points to determine the shape of the quadrilateral. May be square, rectangular, rhombic, parallelogram, trapezoidal or ordinary quadrilateral.
Solution: Start still tangled how to arrange four points sequentially, if the direct processing, a bit of trouble, the original convex bag can be made, directly to a convex bag, and then point on the automatic counter-clockwise row, and then judgment on it, the judge according to the topic below, mainly with point product and cross product, Judge vertical with dot product, judge parallel with cross product.
Code:
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#defineEPS 1e-8using namespacestd;structpoint{Doublex, y; Point (Doublex=0,Doubley=0): X (x), Y (y) {}voidInput () {scanf ("%LF%LF",&x,&y); }};typedef point Vector;structcircle{point C; DoubleR; Circle () {} circle (point C,DoubleR): C (c), R (r) {} point point (DoubleA) {returnPoint (c.x + cos (a) *r, C.y + sin (a) *R); } voidInput () {scanf ("%LF%LF%LF",&c.x,&c.y,&R); }};structline{point P; Vector v; Doubleang; Line () {} line (point P, Vector v):p (P), V (v) {ang=atan2 (v.y,v.x);} Point Point (DoubleT) {returnPoint (p.x + t*v.x, p.y + t*v.y); } BOOL operator< (ConstLine &l)Const{returnAng <L.ang;}};intDCMP (Doublex) {if(x <-eps)return-1; if(X > EPS)return 1; return 0;} Template<classT> T Sqr (t x) {returnX *x;} Vectoroperator+ (vector A, vector B) {returnVector (a.x + b.x, A.Y +b.y); }vectoroperator-(vector A, vector B) {returnVector (a.x-b.x, A.Y-b.y); }vectoroperator* (Vector A,DoubleP) {returnVector (A.x*p, a.y*p); }vectoroperator/(Vector A,DoubleP) {returnVector (a.x/p, a.y/p); }BOOL operator< (Constpoint& A,Constpoint& b) {returna.x < b.x | | (a.x = = b.x && A.y <b.y); }BOOL operator>= (Constpoint& A,Constpoint& b) {returna.x >= b.x && a.y >=b.y;}BOOL operator<= (Constpoint& A,Constpoint& b) {returna.x <= b.x && a.y <=b.y;}BOOL operator== (Constpoint& A,Constpoint& b) {returnDCMP (a.x-b.x) = =0&& dcmp (a.y-b.y) = =0; }DoubleDot (vector A, vector B) {returna.x*b.x + a.y*b.y;}DoubleLength (Vector A) {returnsqrt (Dot (A, a));}DoubleAngle (vector A, vector B) {returnACOs (Dot (A, B)/Length (a)/Length (B)); }DoubleCross (vector A, vector B) {returna.x*b.y-a.y*b.x;} Vector vectorunit (vector x) {returnX/Length (x);} Vector Normal (vector x) {returnPoint (-X.Y, x.x)/Length (x);}DoubleAngle (Vector v) {returnatan2 (V.Y, v.x);}intConvexhull (point* p,intN, point*ch) {Sort (p,p+N); intm =0; for(intI=0; i<n;i++) { while(M >1&& Cross (ch[m-1]-ch[m-2], p[i]-ch[m-2]) <=0) m--; Ch[m++] =P[i]; } intK =m; for(inti=n-2; i>=0; i--) { while(M > K && Cross (ch[m-1]-ch[m-2], p[i]-ch[m-2]) <=0) m--; Ch[m++] =P[i]; } if(N >1) m--; returnm;}//Data SegmentPoint p[5],ch[5]; Point A,b,c,d;//Data EndsintMain () {intT,n,i,cs =1; scanf ("%d",&t); while(t--) { for(i=0;i<4; i++) P[i].input (); printf ("Case %d:", cs++); intm = Convexhull (p,4, CH); if(M <4) {puts ("Ordinary quadrilateral");Continue; } A= ch[0], B = ch[1], C = ch[2], D = ch[3]; if(DCMP (Dot (b-a,d-a)) = =0&& dcmp (Dot (b-a,c-b)) = =0&& dcmp (Dot (c-b,c-d)) = =0&& dcmp (Dot (d-c,d-a)) = =0&& dcmp (Length (b-a)-length (c-b)) = =0&& dcmp (Length (c-b)-length (d-c)) = =0&& dcmp (Length (c-d)-length (a-d)) = =0) puts ("Square"); Else if(DCMP (Dot (b-a,d-a)) = =0&& dcmp (Dot (b-a,c-b)) = =0&& dcmp (Dot (c-b,c-d)) = =0&& dcmp (Dot (d-c,d-a)) = =0&& dcmp (Length (a-d)-length (c-b)) = =0&& dcmp (Length (A-B)-length (c-d)) = =0) puts ("Rectangle"); Else if(DCMP (Length (b-a)-length (c-b)) = =0&& dcmp (Length (c-b)-length (d-c)) = =0&& dcmp (Length (c-d)-length (a-d)) = =0) puts ("Rhombus"); Else if(DCMP (Length (a-d)-length (b-c)) = =0&& dcmp (Length (A-B)-length (c-d)) = =0) puts ("Parallelogram"); Else if(DCMP (Cross (b-c,d-a)) = =0|| DCMP (Cross (b-a,d-c)) = =0) puts ("Trapezium"); Elseputs ("Ordinary quadrilateral"); } return 0;}View Code
UVA 11800 determine the Shape--the first question of convex hull