首页 > 代码库 > UVA11800 - Determine the Shape

UVA11800 - Determine the Shape

每次给4个点坐标,确保没有三点共线,判断构成的是个什么四边形就行了,并输出类型。


我的做法:


以一个点为极点进行极角排序使得点都是逆时针排列


用点积和叉积判断线与线的关系


我的代码:


#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");
		else
			printf("Ordinary Quadrilateral\n");
	}
}
原题:

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
Rhombus
 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 integer T, 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 “Case k: s”.Here k is the case number starting from 1 ands isthe 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 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
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.


UVA11800 - Determine the Shape