[VA] 11800-Determine the Shape (Computational ry)

Source: Internet
Author: User

[VA] 11800-Determine the Shape (Computational ry)

This question is more basic and has many methods. I use the Vector Method for computation.

Enumerate any three points to see if the three vectors identified by the three points and the fourth point constitute a parallelogram. If they are parallelogram, then judge the special graph.

14118653 11800 Determine the Shape Accepted C ++ 0.102 2014-08-30 13:31:48

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include using namespace std; const double eps = 1e-10; struct Point {double x, y; Point (double x = 0, double y = 0): x (x ), y (y) {} bool operator <(const Point & a) const {if (. x! = X) return x <. x; return y <. y ;}}; typedef Point Vector; Vector operator + (Vector A, Vector B) {return Vector (. x + B. x,. y + B. y);} Vector operator-(Point A, Point B) {return Vector (. x-B.x,. y-B.y);} Vector operator * (Vector A, double p) {return Vector (. x * p,. y * p);} Vector operator/(Vector A, double p) {return Vector (. x/p,. y/p);} int dcmp (double x) {if (fabs (x) <eps) return 0; else Return x <0? -1: 1;} bool operator = (const Point & a, const Point & B) {return dcmp (. x-b.x) = 0 & dcmp (. y-b.y) = 0;} double Dot (Vector A, Vector B) {return. x * B. x +. y * B. y;} double Dist (Point A, Point B) {return (. x-B. x) * (. x-B. x) + (. y-B. y) * (. y-B. y);} double Length (Vector A) {return sqrt (Dot (A, A);} double Angle (Vector A, Vector B) {return acos (Dot (, b)/Length (A)/Length (B);} double Cross (Vector A, Vector B) {return. x * B. y-. y * B. x;} double Area2 (Point A, Point B, Point C) {return Cross (B-A, C-A);} Vector Rotate (Vector A, double rad) {return Vector (. x * cos (rad)-. y * sin (rad),. x * sin (rad) +. y * cos (rad);} Point GetIntersection (Point P, Vector v, Point Q, Vector w) {Vector u = P-Q; double t = Cross (w, u) /Cross (v, w); return P + v * t;} bool SegmentProperIntersection (Point a1, Poin T a2, Point b1, Point b2) {double c1 = Cross (a2-a1, b1-a1), c2 = Cross (a2-a1, b2-a1); double c3 = Cross (b2-b1, a1-b1 ), c4 = Cross (b2-b1, a2-b1); return dcmp (c1) * dcmp (c2) <0 & dcmp (c3) * dcmp (c4) <0 ;} double PolygonArea (Point * p, int n) {double area = 0; for (int I = 1; I <n-1; I ++) area + = Cross (p [I]-p [0], p [I + 1]-p [0]); return area;} bool OnSegment (Point p, point a1, Point a2) {return dcmp (Cross (a1-p, A2-p) = 0 & dcmp (Dot (a1-p, a2-p) <0 ;} // Square // Rectangle // fig // Parallelogram // Trapezium trapezoid // Ordinary Quadrilateral normal Quadrilateral Point P [5]; char result [10] [30]; void init () {strcpy (result [1], "Square"); strcpy (result [2], "Rectangle "); strcpy (result [3], "Fig"); strcpy (result [4], "Parallelogram"); strcpy (result [5], "Trapezium "); strcpy (result [6], "Ordinary Quadrilateral");} int Judge (Po Int a, Point B, Point c, Point d) {Vector v1 = B-a; Vector v2 = c-a; Vector v3 = v1 + v2; if (v3 = (d-a) {// It is determined that it is a parallelogram double L1 = Dist (a, B); double L2 = Dist (a, c ); double k = Dot (v1, v2); if (! Dcmp (L1-L2 )&&! Dcmp (k) return 1; if (! Dcmp (k) return 2; if (! Dcmp (L1-L2) return 3; else return 4;} else return 0;} int Judge2 (Point a, Point B, Point c, Point d) {Vector AB = B-a; Vector bc = c-B; Vector cd = d-c; Vector da = a-d; double ans1 = Cross (AB, cd ); double ans2 = Cross (bc, da); if (! Dcmp (ans1) |! Dcmp (ans2) return 1; else return 0;} int solve () {int ans; ans = Judge (P [1], P [2], P [3], P [4]); if (ans) return ans; ans = Judge (P [1], P [3], P [4], P [2]); if (ans) return ans; ans = Judge (P [1], P [2], P [4], P [3]); if (ans) return ans; else {int OK = 0; OK = Judge2 (P [1], P [2], P [3], P [4]); if (OK) return 5; OK = Judge2 (P [1], P [3], P [4], P [2]); if (OK) return 5; OK = Judge2 (P [1], P [2], P [4], P [3]); if (OK) return 5;} return 6 ;} int main () {init (); int T, Case = 1; scanf ("% d", & T); while (T --) {for (int I = 1; I <= 4; I ++) scanf ("% lf", & P [I]. x, & P [I]. y); int ans = solve (); printf ("Case % d: % s \ n", Case ++, result [ans]);} return 0 ;}
         
        
       
      
     
    
   
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.