uva 11800 Determine the Shape

时间:2021-12-03 16:04:27

  vjudge上题目链接:Determine the Shape

  第二道独自 A 出的计算几何水题,题意就是给你四个点,让你判断它是哪种四边形:正方形、矩形、菱形、平行四边形、梯形 or 普通四边形。

  按照各个四边形的特征层层判断就行,只是这题四个点的相对位置不确定(点与点之间是相邻还是相对并不确定),所以需要枚举各种情况,幸好 4 个点一共只有 3 种不同的情况:abcd、abdc、acbd 画个图就能一目了然,接下来就是编码了,无奈因为一些细节问题我还是调试了还一会 o(╯□╰)o

 #include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; struct Vector {
double x,y;
Vector(double x = , double y = ): x(x), y(y) {}
void readint() {
int x,y;
scanf("%d %d",&x,&y);
this->x = x;
this->y = y;
}
Vector operator + (const Vector &b) const {
return Vector(x + b.x, y + b.y);
}
Vector operator - (const Vector &b) const {
return Vector(x - b.x, y - b.y);
}
Vector operator * (double p) const {
return Vector(x * p, y * p);
}
Vector operator / (double p) const {
return Vector(x / p, y / p);
}
}; typedef Vector point;
typedef const point& cpoint;
typedef const Vector& cvector; Vector operator * (double p, const Vector &a) {
return a * p;
} double dot(cvector a, cvector b) {
return a.x * b.x + a.y * b.y;
} double length(cvector a) {
return sqrt(dot(a,a));
} double cross(cvector a, cvector b) {
return a.x * b.y - a.y *b.x;
} const double eps = 1e-;
int dcmp(double x) {
return fabs(x) < eps ? : (x < ? - : );
} bool operator == (cpoint a, cpoint b) {
return dcmp(a.x - b.x) == && dcmp(a.y - b.y) == ;
} const char s[][] = {"Ordinary Quadrilateral", "Trapezium", "Parallelogram",
"Rhombus", "Rectangle", "Square" }; int judge(cpoint a, cpoint b, cpoint c, cpoint d) {
if(dcmp(cross(a - b, c - d)) == ) {
if(dcmp(cross(b - c, a - d)) == ) {
if(dcmp(length(b - a) - length(d - a)) == ) {
if(dcmp(dot(b - a, d - a)) == ) return ;
else return ;
}
else if(dcmp(dot(b - a, d - a)) == ) return ;
else return ;
}
else return ;
}
else if(dcmp(cross(b - c, a - d)) == ) return ;
else return ;
} int main() {
int t,Case = ;
point a,b,c,d;
scanf("%d",&t);
while(t--) {
a.readint();
b.readint();
c.readint();
d.readint();
printf("Case %d: ",++Case);
int ans = ;
ans = max(ans, judge(a,b,c,d));
ans = max(ans, judge(a,b,d,c));
ans = max(ans, judge(a,c,b,d));
printf("%s\n",s[ans]);
}
return ;
}

  计算几何,还是很有趣的。。。