UVA 11800 Determine the Shape（判四边形）

2019年02月12日 ⁄ 综合 ⁄ 共 1808字 ⁄ 字号 评论关闭

```#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>

using namespace std;

char shape[6][30] = {
"Square",
"Rectangle",
"Rhombus",
"Parallelogram",
"Trapezium",
};

const double eps = 1e-8;

int sig(double argu)
{
return (argu > eps) - (argu < -eps);
}

typedef struct Point
{
double x, y;
Point (double _x = 0.0, double _y = 0.0):
x(_x), y(_y) {}
Point operator +(const Point &argu) const
{
return Point(x + argu.x, y + argu.y);
}
Point operator -(const Point &argu) const
{
return Point(x - argu.x, y - argu.y);
}
Point operator *(const double k) const
{
return Point(x * k, y * k);
}
Point operator /(const double k) const
{
return Point(x / k, y / k);
}
double operator ^(const Point &argu) const
{
return (x * argu.y - y * argu.x);
}
double operator *(const Point &argu) const
{
return (x * argu.x + y * argu.y);
}
bool operator <(const Point &argu) const
{
if(sig(y - argu.y) == 0)
return x < argu.x;
else
return y < argu.y;
}
}Vector;

Point vert[4], minv;
Vector edge[4], diag[2];
double x, y;

bool cmp(Point m, Point n)
{
double mang = atan2(m.y - minv.y, m.x - minv.x);
double nang = atan2(n.y - minv.y, n.x - minv.x);
if(sig(mang - nang) == 0)
return m.x < n.x;
else
return mang < nang;
}

void CheckShape()
{
if(sig(edge[0] ^ edge[2]) == 0 || sig(edge[1] ^ edge[3]) == 0)
{
if(sig(edge[0] ^ edge[2]) == 0 && sig(edge[1] ^ edge[3]) == 0)
{
if(sig(diag[0] * diag[1]) == 0)
{
if(sig(edge[0] * edge[1]) == 0)
puts(shape[0]);
else
puts(shape[2]);
}
else if(sig(edge[0] * edge[1]) == 0)
puts(shape[1]);
else
puts(shape[3]);
}
else
puts(shape[4]);
}
else
puts(shape[5]);
}

int main()
{
//freopen("11800.in", "r", stdin);

int n;
scanf("%d", &n);
for(int cas = 1; cas <= n; cas++)
{
printf("Case %d: ", cas);
for(int i = 0; i < 4; i++)
{
scanf("%lf%lf", &x, &y);
vert[i] = Point(x, y);
}
minv = vert[0];
for(int i = 0; i < 4; i++)
if(minv < vert[i])
minv = vert[i];
sort(vert, vert + 4, cmp);
for(int i = 0; i < 4; i++)
edge[i] = vert[i] - vert[(i + 1) % 4];
diag[0] = vert[0] - vert[2];
diag[1] = vert[1] - vert[3];
CheckShape();
}
return 0;
}
```