Each time four coordinate points are given to ensure that there are no three-point collinearity, it is enough to determine what a quadrilateral is formed and the output type.
My practices:
Sorts vertices with a vertex as the pole so that all vertices are arranged counterclockwise.
Determine the relationship between a line and a line using the dot and Cross Product
My code:
#include<iostream>#include<map>#include<string>#include<cstring>#include<cstdio>#include<cstdlib>#include<cmath>#include<queue>#include<vector>#include<algorithm>using namespace std;struct Dot{int x,y;Dot(){}Dot(int a,int b){x=a,y=b;}};typedef pair<Dot,Dot> Seg;Dot operator - (Dot a,Dot b){return Dot(b.x-a.x,b.y-a.y);}int operator * (Dot a,Dot b){return a.x*b.y-b.x*a.y;}int operator / (Dot a,Dot b){return a.x*b.x+a.y*b.y;}Dot KEY;bool cmp(Dot a,Dot b){return (a-KEY)*(b-KEY)>0;}int dis2(Dot a,Dot b){return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}int jd(Seg a,Seg b){if((a.first-a.second)/(b.first-b.second)==0)return 1;Dot t=a.second-a.first;if(t*(b.first-a.first)==t*(b.second-a.first))return -1;return 0;}int main(){Dot dot[4];Seg seg[4];int i,t,len[4],j;cin>>t;for(j=1;j<=t;j++){for(i=0;i<4;i++)cin>>dot[i].x>>dot[i].y;KEY=dot[0];sort(dot+1,dot+4,cmp);for(i=0;i<3;i++)seg[i]=make_pair(dot[i],dot[i+1]);seg[i]=make_pair(dot[i],dot[0]);for(i=0;i<4;i++)len[i]=dis2(seg[i].first,seg[i].second);printf("Case %d: ",j);if(len[0]==len[1]&&len[1]==len[2]&&len[2]==len[3]&&jd(seg[0],seg[1])==1&&jd(seg[1],seg[2])==1&&jd(seg[2],seg[3])==1&&jd(seg[3],seg[0])==1)printf("Square\n");else if(len[0]==len[2]&&len[1]==len[3]&&jd(seg[0],seg[1])==1&&jd(seg[1],seg[2])==1&&jd(seg[2],seg[3])==1&&jd(seg[3],seg[0])==1)printf("Rectangle\n");else if(len[0]==len[1]&&len[1]==len[2]&&len[2]==len[3]&&jd(seg[0],seg[1])!=1&&jd(seg[1],seg[2])!=1&&jd(seg[2],seg[3])!=1&&jd(seg[3],seg[0])!=1)printf("Rhombus\n");else if(len[0]==len[2]&&len[1]==len[3]&&jd(seg[0],seg[1])!=1&&jd(seg[1],seg[2])!=1&&jd(seg[2],seg[3])!=1&&jd(seg[3],seg[0])!=1)printf("Parallelogram\n");else if(jd(seg[0],seg[2])==-1||jd(seg[1],seg[3])==-1)printf("Trapezium\n");elseprintf("Ordinary Quadrilateral\n");}}
Original question:
Time Limit: 1.000 seconds
A toycompany recently found that toys like revolver, machine guns, fighting planesare making children violent and destroying the peace of the world. the parentsalso began to avoid these toys and inclined to educational toys. so theydecided to manufacture educational toys. one of these is a electric touch padon which children can put four points and the program will automatically jointhe points to form a closed shape. children will try to guess the shape andwhen they press a button then it will automatically announce the shape. butthey are struggling to determine the shape and seek your help.
Your task is simple. You are given four points, nothree of them are collinear, You have to output the simple polygonal shapeformed by these points in the following order:
Square
Rectangle
Fig
Parallelogram
Trapezium
Ordinary Quadrilateral
For example if it is possible to form a square withthe four points you must output 'square ', if it is notpossible to form a square but possible to form a rectangle you must output 'rectangle 'andso on.
Input
Input starts with an integerT, The number of test cases (T ≤ 50000). Each testcase contains 4 lines. Each of the lines contains two space separated IntegersXi Yi (-10000 ≤ Xi, Yi ≤ 10000)Which areThe coordinate values of a point.
Output
For each set of input outputone line in the format"CaseK:S". HereKIs the case number starting from 1 andSIsthe shape as described above. See sample input output for more details.
Sample Input |
Sample output |
6 0 0 2 0 2 2 0 2 0 0 3 0 3 2 0 2 0 0 8 4 5 0 3 4 0 0 2 0 3 2 1 2 0 0 5 0 4 3 1 3 0 0 5 0 4 3 1 4 |
Case 1: Square Case 2: rectangle Case 3: fig Case 4: Parallelogram Case 5: trapezium Case 6: ordinary Quadrilateral |
Note: If you have forgotten elementarygeometry, here is the definitions to remind you:
Square: all sides are ofequal size all angles are 90o
Rectangle: opposite sides are of equal size and all angles are 90o
Fig: all sides are of equal size but no angle is 90o
Parallelogram: opposite sides are of equal size but no angle is 90o
Trapezium: any two opposite sides are parallel but the other two is not.
Simple Polygon: polygon having no self intersecting edge.
Uva11800-determine the shape