首页 > 代码库 > UVA 11800 Determine the Shape --凸包第一题
UVA 11800 Determine the Shape --凸包第一题
题意: 给四个点,判断四边形的形状。可能是正方形,矩形,菱形,平行四边形,梯形或普通四边形。
解法: 开始还在纠结怎么将四个点按序排好,如果直接处理的话,有点麻烦,原来凸包就可搞,直接求个凸包,然后点就自动按逆时针排好了,然后就判断就可以了,判断依据题目下面有,主要是用到点积和叉积,判断垂直用点积,判断平行用叉积。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#define eps 1e-8using namespace std;struct Point{ double x,y; Point(double x=0, double y=0):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 -1; if(x > eps) return 1; return 0;}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) == 0 && dcmp(a.y-b.y) == 0; }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 = 0; for(int i=0;i<n;i++) { while(m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--; ch[m++] = p[i]; } int k = m; for(int i=n-2;i>=0;i--) { while(m > k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--; ch[m++] = p[i]; } if(n > 1) m--; return m;}//data segmentPoint p[5],ch[5];Point A,B,C,D;//data endsint main(){ int t,n,i,cs = 1; scanf("%d",&t); while(t--) { for(i=0;i<4;i++) p[i].input(); printf("Case %d: ",cs++); int m = ConvexHull(p,4,ch); if(m < 4) { puts("Ordinary Quadrilateral"); continue; } A = ch[0], B = ch[1], C = ch[2], D = ch[3]; if(dcmp(Dot(B-A,D-A)) == 0 && dcmp(Dot(B-A,C-B)) == 0 && dcmp(Dot(C-B,C-D)) == 0 && dcmp(Dot(D-C,D-A)) == 0 && dcmp(Length(B-A)-Length(C-B)) == 0 && dcmp(Length(C-B)-Length(D-C)) == 0 && dcmp(Length(C-D)-Length(A-D)) == 0) puts("Square"); else if(dcmp(Dot(B-A,D-A)) == 0 && dcmp(Dot(B-A,C-B)) == 0 && dcmp(Dot(C-B,C-D)) == 0 && dcmp(Dot(D-C,D-A)) == 0 && dcmp(Length(A-D)-Length(C-B)) == 0 && dcmp(Length(A-B)-Length(C-D)) == 0) puts("Rectangle"); else if(dcmp(Length(B-A)-Length(C-B)) == 0 && dcmp(Length(C-B)-Length(D-C)) == 0 && dcmp(Length(C-D)-Length(A-D)) == 0) puts("Rhombus"); else if(dcmp(Length(A-D)-Length(B-C)) == 0 && dcmp(Length(A-B)-Length(C-D)) == 0) puts("Parallelogram"); else if(dcmp(Cross(B-C,D-A)) == 0 || dcmp(Cross(B-A,D-C)) == 0) puts("Trapezium"); else puts("Ordinary Quadrilateral"); } return 0;}
UVA 11800 Determine the Shape --凸包第一题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。