Determine the Shape | |
Input | Standard Input |
Output | Standard Output |
A toy company recently found that toys like revolver, machine guns, fighting planes are making children violent and destroying the peace of the world. The parents also began to avoid these toys and inclined to educational toys. So they decided to manufacture educational toys. One of these is a electric touch pad on which children can put four points and the program will automatically join the points to form a closed shape. Children will try to guess the shape and when they press a button then it will automatically announce the shape. But they are struggling to determine the shape and seek your help.
Your task is simple. You are given four points, no three of them are collinear, you have to output the simple polygonal shape formed by these points in the following order:
Square
Rectangle
Rhombus
Parallelogram
Trapezium
Ordinary Quadrilateral
For example if it is possible to form a square with the four points you must output ‘Square’, if it is not possible to form a square but possible to form a rectangle you must output ‘Rectangle’ and so on.
Input
Input starts with an integer T, the number of test cases (T≤50000). Each test case contains 4 lines. Each of the lines contains two space separated integers xi yi (-10000≤xi, yi≤ 10000) which are the coordinate values of a point.
Output
For each set of input output one line in the format “Case k: s”. Here k is the case number starting from 1 and s is the 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: Rhombus Case 4: Parallelogram Case 5: Trapezium Case 6: Ordinary Quadrilateral
|
Note: If you have forgotten elementary geometry, here is the definitions to remind you:
Square: All sides are of equal size all angles are 90o
Rectangle: Opposite sides are of equal size and all angles are 90o
Rhombus: 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.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
struct Point{
int x,y;
}vis[4];
double l[4],a[4];
typedef Point Vector ;
Vector operator - (Point A , Point B){
A.x-=B.x;
A.y-=B.y;
return A;
}
double Dot(Vector A , Vector B){
return A.x*B.x+A.y*B.y;
}
double Cross(Vector A , Vector B){
return A.x*B.y - A.y*B.x;
}
double Area2(Point A ,Point B ,Point C){
return Cross(B-A, C-A);
}
double juli(Point A, Point B)
{
return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}
int main()
{
int t,cas=1;;
scanf("%d",&t);
while(t--)
{
int i,j,k,p,flag=0;
for(i=0;i<4;i++)
scanf("%d%d",&vis[i].x,&vis[i].y);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
{
for(p=0;p<4;p++)
{
if(i!=j&&k!=i&&k!=j&&p!=i&&p!=j&&p!=k)
{
//cout<<i<<j<<k<<p<<endl;
//cout<<Area2(vis[k],vis[i],vis[j])<<endl;
//cout<<Area2(vis[p],vis[i],vis[j])<<endl;
if((Area2(vis[k],vis[i],vis[j])>0&&Area2(vis[p],vis[i],vis[j])<0)||(Area2(vis[k],vis[i],vis[j])<0&&Area2(vis[p],vis[i],vis[j])>0))
{
flag=1;
break;
}
}
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
//cout<<i<<j<<k<<p<<endl;
l[0]=juli(vis[i],vis[k]);
l[1]=juli(vis[i],vis[p]);
l[2]=juli(vis[p],vis[j]);
l[3]=juli(vis[j],vis[k]);
a[0]=Dot(vis[i]-vis[k],vis[i]-vis[p]);
a[1]=Dot(vis[p]-vis[i],vis[p]-vis[j]);
a[2]=Dot(vis[j]-vis[k],vis[j]-vis[p]);
a[3]=Dot(vis[k]-vis[i],vis[k]-vis[j]);
printf("Case %d: ",cas++);
if(l[0]==l[1]&&l[1]==l[2]&&l[2]==l[3]&&a[0]==0&&a[1]==0&&a[2]==0&&a[3]==0)
cout<<"Square\n";
else
if(l[0]==l[2]&&l[1]==l[3]&&a[0]==0&&a[1]==0&&a[2]==0&&a[3]==0)
cout<<"Rectangle\n";
else
if(l[0]==l[1]&&l[1]==l[2]&&l[2]==l[3])
cout<<"Rhombus\n";
else
if(l[0]==l[2]&&l[1]==l[3])
cout<<"Parallelogram\n";
else
if(Cross(vis[i]-vis[p],vis[k]-vis[j])==0||Cross(vis[i]-vis[k],vis[p]-vis[j])==0)
cout<<"Trapezium\n";
else
cout<<"Ordinary Quadrilateral\n";
}
return 0;
}