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.
题意:每组样例给你四个点,按优先顺序判断是否为正方形、矩形、菱形、平行四边形、梯形、普通四边形。
思路:首先用求凸包的算法按逆时针给四个点排序,如果不能构成凸包的话,那么就是普通四边形。只是凸包的算法需要学习一下,剩下的判断都很简单。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct Point
{
int x,y;
Point(int x=0,int y=0):x(x),y(y){}
};
typedef Point Vector;
bool cmp(Point a,Point b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
Vector operator + (Vector A,Vector B) {return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (Vector A,Vector B) {return Vector(A.x-B.x,A.y-B.y);}
Vector operator * (Vector A,double p) {return Vector(A.x*p,A.y*p);}
Vector operator / (Vector A,double p) {return Vector(A.x/p,A.y/p);}
int Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;}
int Length(Vector A){return Dot(A,A);}
int Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;}
int ConvexHull(Point *p,Point *ch,int n)
{
sort(p,p+n,cmp);
int i,m=0,k;
for(i=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];
}
k=m;
for(i=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--;
return m;
}
Point p[5],ch[5];
int main()
{
int T,t,i,j,k;
scanf("%d",&T);
for(t=1;t<=T;t++)
{
for(i=0;i<4;i++)
scanf("%d%d",&p[i].x,&p[i].y);
k=ConvexHull(p,ch,4);
printf("Case %d: ",t);
if(k<4)
{
printf("Ordinary Quadrilateral\n");
continue;
}
if(Cross(ch[1]-ch[0],ch[3]-ch[2])==0 && Cross(ch[3]-ch[0],ch[1]-ch[2])==0)
{
if(Dot(ch[1]-ch[0],ch[3]-ch[0])==0)
{
if(Length(ch[1]-ch[0])==Length(ch[3]-ch[0]))
printf("Square\n");
else
printf("Rectangle\n");
}
else
{
if(Length(ch[1]-ch[0])==Length(ch[3]-ch[0]))
printf("Rhombus\n");
else
printf("Parallelogram\n");
}
}
else if(Cross(ch[1]-ch[0],ch[3]-ch[2])==0 || Cross(ch[3]-ch[0],ch[1]-ch[2])==0)
printf("Trapezium\n");
else
printf("Ordinary Quadrilateral\n");
}
}