首页 > 其他 > 详细

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

时间:2014-12-11 23:58:14      阅读:570      评论:0      收藏:0      [点我收藏+]

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

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

代码:

bubuko.com,布布扣
#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=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 segment
Point p[5],ch[5];
Point A,B,C,D;
//data ends

int 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;
}
View Code

 

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

原文:http://www.cnblogs.com/whatbeg/p/4158726.html

(1)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!