uva 10652(凸包)

题意:有n块矩形木板,用一个面积尽量小的凸多边形把它们包起来,并计算出木板占整个包装面积的百分比。
题解:把每个木板旋转后的四个点加入集合,最后就有4×n个点的点集,然后问题转化为普通的凸包问题就容易解决了。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double PI = acos(-1);
const int N = 2500;
struct Point {
    double x, y;
    Point(double x = 0, double y = 0): x(x), y(y) {}
}P[N], res[N];
struct Circle {
    Point c;
    double r;
    Circle() {}
    Circle(Point c, double r = 0): c(c), r(r) {}
    Point point(double a) {
        return Point(c.x + cos(a) * r, c.y + sin(a) * c.y);
    }
};
int n, cnt;
double Sqr(double x) {
    return x * x;
}
Point operator + (Point A, Point B) {
    return Point(A.x + B.x, A.y + B.y);
}
Point operator - (Point A, Point B) {
    return Point(A.x - B.x, A.y - B.y);
}
Point operator * (Point A, double p) {
    return Point(A.x * p, A.y * p);
}
Point operator / (Point A, double p) {
    return Point(A.x / p, A.y / p);
}
//计算点积的正负  负值夹角为钝角
int dcmp(double x) {
    if (fabs(x) < 1e-9)
        return 0;
    return x < 0 ? -1 : 1;
}
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 dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}
//计算点积
double Dot(Point A, Point B) {
    return A.x * B.x + A.y * B.y;
}
//计算叉积,也就是数量积
double Cross(Point A, Point B) {
    return A.x * B.y - A.y * B.x;
}
//计算向量长度
double Length(Point A) {
    return sqrt(Dot(A, A));
}
//向量A旋转rad弧度,rad负值为顺时针旋转
Point Rotate(Point A, double rad) {
    return Point(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}
//角度转化弧度
double torad(double deg) {
    return deg / 180.0 * PI;
}
//得到凸包点数组和顶点数
int ConvexHull(Point* P, int cnt, Point* res) {
    sort(P, P + cnt);
    cnt = unique(P, P + cnt) - P;
    int m = 0;
    for (int i = 0; i < cnt; i++) {
        while (m > 1 && Cross(res[m - 1] - res[m - 2], P[i] - res[m - 2]) <= 0)
            m--;
        res[m++] = P[i];
    }
    int k = m;
    for (int i = cnt - 2; i >= 0; i--) {
        while (m > k && Cross(res[m - 1] - res[m - 2], P[i] - res[m - 2]) <= 0)
            m--;
        res[m++] = P[i];
    }
    if (cnt > 1)
        m--;
    return m;
}
//计算多边形面积
double PolygonArea(Point* res, int m) {
    double area = 0;
    for (int i = 1; i < m - 1; i++)
        area += Cross(res[i] - res[0], res[i + 1] - res[0]);
    return area / 2;
}

int main() {
    Point P[N], res[N];
    int t;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        int cnt = 0;
        double x, y, w, h, j, sum1 = 0;
        for (int i = 0; i < n; i++) {
            scanf("%lf%lf%lf%lf%lf", &x, &y, &w, &h, &j);   
            Point p(x, y);
            double ang = -torad(j);
            P[cnt++] = p + Rotate(Point(w / 2, h / 2), ang);
            P[cnt++] = p + Rotate(Point(w / 2, -h / 2), ang);
            P[cnt++] = p + Rotate(Point(-w / 2, h / 2), ang);
            P[cnt++] = p + Rotate(Point(-w / 2, -h / 2), ang);
            sum1 += w * h;
        }
        int temp = ConvexHull(P, cnt, res);
        double sum2 = PolygonArea(res, temp);
        printf("%.1lf %%\n", sum1 * 100 / sum2);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值