Hihocoder #1040 Rectangle (calculates the coordinates of a geometry question to 8 points, can be a rectangular "template idea")

Source: Internet
Author: User

#1040: The rectangle determines the time limit:1000msSingle Point time limit:1000msMemory Limit:256MBDescribe

The 4 lines on the plane are given to determine whether the 4 segments are exactly enclosed in a rectangle with an area greater than 0.

Input

The first line of input is an integer T (1<=t<=100), which represents the number of test data.

Each set of data contains 4 rows, each containing 4 integers x1, y1, x2, y2 (0 <= x1, y1, x2, y2 <= 100000), where (x1, y1), (X2,y2) represents two endpoints of a line segment.

Output

Each set of data outputs a row of Yes or no, indicating whether the input 4 segments are exactly rectangular.

Sample input
30 0 0 11 0 1 10 1 1 11 0 0 00 1 2 31 0 3 23 2 2 31 0 0 10 1 1 01 0 2 02 0 1 11 1 0 1
Sample output
 Yesyesno 
algorithm analysis: I do not have the geometry of the template, the computational geometry of the basic problem can only knock on its own!

To complete this question: First you need to know the knowledge point!
1. A rectangle with an area greater than 0 will have 4 distinct vertices
2. The weights of the four edges are equal (that is, the neighboring edges are equal, four sides of the equilateral parallelogram) or the weights of the edges have only two values (i.e., the edge is unequal, and the is equal to the edge). parallelogram)
3. Finally determine if there is a corner is right-angled (as long as two edges are found perpendicular to each other, that is, the dot product operation of the vector)

Note: I have used the STL's set structure in the implementation of the algorithm, one thing to note is that if you want to introduce a struct to a set set, you must have all the elements of the struct The
makes the writing of an overloaded operator. Otherwise, it will result in the loss of data!
For example: If I insert a point (0, 0) and then go to the insertion point (0,1), it may lose (0, 1) points. Attention!
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include < iostream> #include <string> #include <queue> #include <stack> #include <set> #include < Algorithm> #define EPS 1e-8#define PI acos ( -1.0) using namespace std;struct pointer{int x, Y;bool operator < (const POI        NTER&AMP;DD) const{if (x==dd.x) {return y<dd.y; } return x<dd.x;}} U, v;set<pointer>a; Point Set set<int>b; Edge set int line (pointer A, pointer b) {return ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));} struct vect{int x, y;}    C[4];int Main () {int t;int I, j;scanf ("%d", &t), while (t--) {if (!a.empty ()) a.clear (); if (!b.empty ()) b.clear (); for (i=0; i<4; i++) {scanf ("%d%d%d", &u.x, &u.y, &v.x, &AMP;V.Y);//Read into an edge A.ins ert (U);            A.insert (v);            B.insert (Line (U, v));            c[i].x = u.x-v.x; C[i].y = U.Y-V.Y; Build Vector}if (a.size ()!=4) {Set<pointer>::iterator it=A.begin (); /* while (It!=a.end ()) {printf ("%d--%d", It->x, It->y);            it++;                        } *///printf ("*******%d\n", A.size ()); printf ("no\n");        Continue        } if (B.size () >2) {//==1 is a square ==2 is a rectangular printf ("no\n"); continue;        }//If this quadrilateral has only four points and only one or two different-sized sides bool Flag=false; For (i=0, i<4; i++) {for (j=0; j<4; J + +) {if (i!=j) {if (c[i].x*c[                    j].x + c[i].y*c[j].y) ==0) {flag=true; break;        }}} if (flag==true) break;        } if (flag==true) printf ("yes\n"); else printf ("no\n");} return 0;}

Hihocoder #1040 Rectangle (calculates the coordinates of a geometry question to 8 points, can be a rectangular "template idea")

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.