UVA 11800 Determine the Shape --凸包第一题

时间:2025-05-09 20:05:44

题意: 给四个点,判断四边形的形状。可能是正方形,矩形,菱形,平行四边形,梯形或普通四边形。

解法: 开始还在纠结怎么将四个点按序排好,如果直接处理的话,有点麻烦,原来凸包就可搞,直接求个凸包,然后点就自动按逆时针排好了,然后就判断就可以了,判断依据题目下面有,主要是用到点积和叉积,判断垂直用点积,判断平行用叉积。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define eps 1e-8
using namespace std; struct Point{
double x,y;
Point(double x=, double y=):x(x),y(y) {}
void input() { scanf("%lf%lf",&x,&y); }
};
typedef Point Vector;
struct Circle{
Point c;
double r;
Circle(){}
Circle(Point c,double r):c(c),r(r) {}
Point point(double a) { return Point(c.x + cos(a)*r, c.y + sin(a)*r); }
void input() { scanf("%lf%lf%lf",&c.x,&c.y,&r); }
};
struct Line{
Point p;
Vector v;
double ang;
Line(){}
Line(Point p, Vector v):p(p),v(v) { ang = atan2(v.y,v.x); }
Point point(double t) { return Point(p.x + t*v.x, p.y + t*v.y); }
bool operator < (const Line &L)const { return ang < L.ang; }
};
int dcmp(double x) {
if(x < -eps) return -;
if(x > eps) return ;
return ;
}
template <class T> T sqr(T x) { return x * x;}
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ; }
double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
Vector VectorUnit(Vector x){ return x / Length(x);}
Vector Normal(Vector x) { return Point(-x.y, x.x) / Length(x);}
double angle(Vector v) { return atan2(v.y, v.x); }
int ConvexHull(Point* p, int n, Point* ch)
{
sort(p,p+n);
int m = ;
for(int i=;i<n;i++) {
while(m > && Cross(ch[m-]-ch[m-], p[i]-ch[m-]) <= ) m--;
ch[m++] = p[i];
}
int k = m;
for(int i=n-;i>=;i--) {
while(m > k && Cross(ch[m-]-ch[m-], p[i]-ch[m-]) <= ) m--;
ch[m++] = p[i];
}
if(n > ) m--;
return m;
} //data segment
Point p[],ch[];
Point A,B,C,D;
//data ends int main()
{
int t,n,i,cs = ;
scanf("%d",&t);
while(t--)
{
for(i=;i<;i++) p[i].input();
printf("Case %d: ",cs++);
int m = ConvexHull(p,,ch);
if(m < ) { puts("Ordinary Quadrilateral"); continue; }
A = ch[], B = ch[], C = ch[], D = ch[]; if(dcmp(Dot(B-A,D-A)) == && dcmp(Dot(B-A,C-B)) == && dcmp(Dot(C-B,C-D)) == && dcmp(Dot(D-C,D-A)) ==
&& dcmp(Length(B-A)-Length(C-B)) == && dcmp(Length(C-B)-Length(D-C)) == && dcmp(Length(C-D)-Length(A-D)) == )
puts("Square");
else if(dcmp(Dot(B-A,D-A)) == && dcmp(Dot(B-A,C-B)) == && dcmp(Dot(C-B,C-D)) == && dcmp(Dot(D-C,D-A)) ==
&& dcmp(Length(A-D)-Length(C-B)) == && dcmp(Length(A-B)-Length(C-D)) == )
puts("Rectangle");
else if(dcmp(Length(B-A)-Length(C-B)) == && dcmp(Length(C-B)-Length(D-C)) == && dcmp(Length(C-D)-Length(A-D)) == )
puts("Rhombus");
else if(dcmp(Length(A-D)-Length(B-C)) == && dcmp(Length(A-B)-Length(C-D)) == )
puts("Parallelogram");
else if(dcmp(Cross(B-C,D-A)) == || dcmp(Cross(B-A,D-C)) == )
puts("Trapezium");
else
puts("Ordinary Quadrilateral");
}
return ;
}