UVa 11800 - Determine the Shape

思路一定要清晰 , 题意很简单 , 但是如果一个个的讨论肯定是繁琐的 , 还记得初中课本上怎么定义这些四边形的么 , 按照顺序一个个的来




注意: 四个点的顺序是任意的 , 所以需要极角排序 , 你可以使用叉乘+象限排序的方式  , 但是博主偷懒使用了atan2 , 可能会有精度问题但只有4个点是吧


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <deque>
#include <stack>
#include <algorithm>

using namespace std;
double eps = 1e-10;

int dcmp(double a , double b) { return (fabs(a-b)<=eps)?0:(a<b?-1:1); }

struct points 
{
	int x , y;
	points(int x = 0, int y = 0):x(x),y(y){}
}p[4];

typedef points Vector ;

Vector  operator +(points a , points b) { return Vector(a.x+b.x , a.y+b.y); }
Vector  operator -(points a , points b) { return Vector(a.x-b.x , a.y-b.y); }
Vector  operator /(points a , int    b) { return Vector(a.x/b   , a.y/b  ); }
int Dot(Vector a , Vector b) { return a.x*b.x+a.y*b.y; }

Vector orig;

int p2(int a) { return a*a; }

bool cmp(points& a , points& b)
{
	double t1 = atan2(1.0*a.x-orig.x, 1.0*a.y-orig.y);
	double t2 = atan2(1.0*b.x-orig.x, 1.0*b.y-orig.y);
	return t1<t2; 
}

bool parell(Vector a , Vector b)
{
	return a.x*b.y == a.y*b.x; 
}

void solve()
{
	orig = (p[0]+p[1])/2; 
	sort(p , p+4 , cmp);
	if(parell(p[0]-p[1], p[2]-p[3]) || parell(p[0]-p[3], p[1]-p[2]))
	{
		if(parell(p[0]-p[1], p[2]-p[3]) && parell(p[0]-p[3], p[1]-p[2]))
		{
			bool eq = ( p2(p[0].x-p[1].x)+p2(p[0].y-p[1].y) == p2(p[1].x-p[2].x)+p2(p[1].y-p[2].y) );
			bool re = (Dot(p[0]-p[1], p[0]-p[3])==0) ;
			
			if(eq && re) { cout<<"Square\n"; return; }
			else if(eq ) { cout<<"Rhombus\n"; return;}
			else if(re ) { cout<<"Rectangle\n"; return; }
			else { cout<<"Parallelogram\n";     return; }
		}
		else { cout<<"Trapezium\n"; return ; }
	}
	else { cout<<"Ordinary Quadrilateral\n"; return ; }
}

int main(int argc, char *argv[]) {
	
	int t;
	scanf("%d",&t);
	
	for(int Case=1;Case<=t;Case++)
	{
		for(int i=0;i<4;i++) scanf("%d%d",&p[i].x,&p[i].y);
		cout<<"Case "<<Case<<": ";
		solve();	
	}
	
	return 0;
}



你可能感兴趣的:(计算几何)