题目链接:uva 11800 - Determine the Shape
题意:给定平面上的4个点,没有3点共线,你的任务是判断着4个点能组成什么样的四边形。(正方形,矩形,菱形,平行四边形,菱形,梯形,普通四边形)。
分析:四个点,两两组合可以形成六个边,如果不存在一组边平行那么一定是普通四边形,如果只存在一组平行边那么一定是梯形,如果存在两组平行边继续讨论;如果存在五组边相互垂直,一定是正方形;如果存在四组边相互垂直,一定是矩形;如果只存在一组边相互垂直,一定是菱形,其余情况都是平行四边形。
注意:精度误差,一定不要存在除法和开根号。
#include<stdio.h>
#include<string.h>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
struct Vector2
{
double x,y;
Vector2(){}
Vector2(double tx,double ty){x = tx;y = ty;}
double len(){ return x*x + y*y; }
static double Cos(Vector2& a,Vector2& b)
{
return a.x * b.x + a.y * b.y;
}
static double Dot(Vector2& a,Vector2& b)
{
return a.x * b.y - a.y * b.x;
}
static double nDis(Vector2& a,Vector2& b)
{
double tx = a.x - b.x;
double ty = a.y - b.y;
return tx*tx + ty*ty;
}
static double Dis(Vector2& a,Vector2& b)
{
double tx = a.x - b.x;
double ty = a.y - b.y;
return sqrt(tx*tx + ty*ty);
}
friend istream& operator >> (istream& in,Vector2& a);
friend Vector2 operator - (Vector2& a,Vector2& b);
friend Vector2 operator + (Vector2& a,Vector2& b);
};
istream& operator >> (istream& in,Vector2& a)
{
in >> a.x >> a.y;
return in;
}
Vector2 operator - (Vector2& a,Vector2& b)
{
return Vector2(a.x - b.x,a.y - b.y);
}
Vector2 operator + (Vector2& a,Vector2& b)
{
return Vector2(a.x + b.x,a.y + b.y);
}
int main()
{
int t,Case = 0;
cin >> t;
Vector2 a[4],b[6];
while(t--)
{
cin >> a[0] >> a[1] >> a[2] >> a[3];
printf("Case %d: ",++Case);
for(int i = 0 ; i < 4 ; i ++)
b[i] = a[i] - a[(i+1)%4];
b[4] = a[0] - a[2];
b[5] = a[1] - a[3];
int px = 0,cz = 0,xd = 0;
for(int i = 0 ; i < 6 ; i ++)
for(int j = i + 1 ; j < 6 ; j ++)
{
if(Vector2::Cos(b[i],b[j]) * Vector2::Cos(b[i],b[j]) == b[i].len() * b[j].len())px ++;
if(Vector2::Cos(b[i],b[j]) == 0)cz ++;
}
if(px == 2)
{
if(cz >= 4)
{
if(cz == 4) printf("Rectangle\n");
else printf("Square\n");
}
else
{
if( cz == 1) printf("Rhombus\n");
else printf("Parallelogram\n");
}
}
else if( px == 1)printf("Trapezium\n");
else printf("Ordinary Quadrilateral\n");
}
return 0 ;
}