uva 10652 Board Wrapping(Graham凸包算法)

reference:

http://www.cnblogs.com/absolute8511/archive/2009/08/09/1649565.html

        http://geomalgorithms.com/a10-_hull-1.html

rujia哥哥不是说给的是andrew算法吗,肿么觉得就是原始的graham。。

一个裸凸包就行了,加上向量旋转,多边形求面积~~

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cassert>
#include <algorithm>
#include <cmath>
#include <limits>
#include <set>
#include <map>

using namespace std;

#define MIN(a, b) a < b ? a : b
#define MAX(a, b) a > b ? a : b
#define F(i, n) for (int i=0;i<(n);++i)
#define REP(i, s, t) for(int i=s;i<=t;++i)
#define IREP(i, s, t) for(int i=s;i>=t;--i)
#define REPOK(i, s, t, o) for(int i=s;i<=t && o;++i)
#define MEM0(addr, size) memset(addr, 0, size)
#define LBIT(x) x&-x

#define PI 3.1415926535897932384626433832795
#define HALF_PI 1.5707963267948966192313216916398

#define MAXN 3000
#define MAXM 100
#define MOD 20071027

typedef long long LL;

const double maxdouble = numeric_limits<double>::max();
const double eps = 1e-10;
const int INF = 0x7FFFFFFF;

struct Point {
    double x, y;
    Point(){};
    Point(double x, double y):x(x),y(y){};
};
typedef Point Vector;

Vector operator - (Point A, Point B) {
    return Vector(A.x-B.x, A.y - B.y);
}
Vector operator + (Point A, Point 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);
}
// 用以避免精度误差
int dcmp(double x) {
    if (fabs(x) < eps)
        return 0;
    else
        return x < 0 ? -1 : 1;
}

bool operator == (const Point& a, const Point& b) {
    if (dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0)
        return true;
    return false;
}
// 需要更高精度,使用dcmp
bool operator < (const Point& a, const Point& b) {
    return a.x < b.x || (a.x == b.x && a.y < b.y);
}
// 点积
double Dot(Vector A, Vector B) {
    return A.x*B.x + A.y*B.y;
}
// 叉积
double Cross(Vector A, Vector B) {
    return A.x*B.y-A.y*B.x;
}
// 模
double Length(Vector A) {
    return sqrt(Dot(A, A));
}
double Area2(Point A, Point B, Point C) {
    return Cross(B-A, C-A);
}
// 两向量夹角
double Angle(Vector A, Vector B) {
    return acos(Dot(A, B) / Length(A) / Length(B));
}
// 将向量A]逆时针旋转rad度
Vector Rotate(Vector A, double rad) {
    return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));
}
// 单位法线 左转90度后归一化
Vector Normal(Vector A) {
    double L = Length(A);
    return Vector(-A.y/L, A.x/L);
}
// 相交直线求交点(点向式)
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) {
    Vector u = P - Q;
    double t = Cross(w, u) / Cross(v, w);
    return P + v*t;
}
// 判断线段相交(不含端点)
bool SeqmentProperIntersection(Point a1, Point a2, Point b1, Point b2) {
    double c1 = Cross(a2-a1, b2-a1), c2 = Cross(a2-a1, b1-a1),
           c3 = Cross(b2-b1, a2-b1), c4 = Cross(b2-b1, a1-b1);
    if (dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0)
        return true;
    return false;
}
// 点在线上
bool OnSegment(Point p, Point a1, Point a2) {
    return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0;
}
// 点到线段距离
double DistanceToSegment(Point P, Point A, Point B) {
    if (A == B)
        return Length(P - A);
    Vector v1 = B - A, v2 = P - A, v3 = P - B;
    if (dcmp(Dot(v1, v2)) < 0) // 投影在A的外侧
        return Length(v2); // 投影在B的外侧
    else if (dcmp(Dot(v1, v3)) > 0)
        return Length(v3);
    else
        return fabs(Cross(v1, v2)) / Length(v1);
}
// 余弦定理 1,2为临边, 3为对边
double cos_theorem(double edge1, double edge2, double edge3) {
    return acos((pow(edge1, 2.0) + pow(edge2, 2.0) - pow(edge3, 2.0)) / (2*edge1*edge2));
}
// 多边形有向面积
// 输入为逆时针定点序列
double PolygonArea(Point *p, int n) {
    double area = 0;
    for (int i=1;i<n-1;++i)
        area += Cross(p[i]-p[0], p[i+1]-p[0]);
    return area/2;
}
// Graham凸包算法nlogn
// p中元素顺序会被破坏
// 返回逆时针的定点序列
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;
}
// ccw = (B-A) X (C-A)
// ret > 0 : C 在AB左侧
// ret < 0 : C 在AB右侧
#define ccw(A, B, C) (Cross((B)-(A),(C)-(A)))

Point pts[MAXN + 1];
Point v[MAXN + 1];
int main()
{
    freopen("input.in", "r", stdin);
    int N,n, cnt;
    double x,y,w,h,theta;
    double area1, area2, ang;

    cin >> N;
    F(ncases, N) {
        cin >> n;
        area1 = 0;
        cnt = 0;
        F(i, n) {
            cin >> x >> y >> w >> h >> theta;
            area1 += w*h;
            ang = -theta / 180 * PI;
            Point o(x, y);
            pts[cnt++] = (o + Rotate(Vector(-w/2,-h/2), ang));
            pts[cnt++] = (o + Rotate(Vector(-w/2,h/2), ang));
            pts[cnt++] = (o + Rotate(Vector(w/2,-h/2), ang));
            pts[cnt++] = (o + Rotate(Vector(w/2,h/2), ang));
        }
        sort(pts, pts+cnt);
        int num_v = ConvexHull(pts, cnt, v);
        area2 = PolygonArea(v, num_v);
        printf("%.1f \%\n", area1/area2*100);
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值