UVA - 10652 Board Wrapping(计算几何-凸包)

35 篇文章 0 订阅
2 篇文章 0 订阅

点击打开题目链接

 

第一次做计算几何的题,真切感受到数学不好是最致命的,半天连一个角度化弧度都想不出(可以看出我数学有多坨 TAT ),题目本身理解不难,但是需要各种功能函数的实现,组合。计算几何的题以后还是要多做。

题目大意:用一个尽量小的凸多边形包起n块矩形木板,计算木板面积与整个包装面积的百分比。

思路:每块木板的四个点都作为输入,求出的凸包就是包装

附上AC代码:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstdio>

using namespace std;
const int maxn = 600 * 4 + 5;
int T;
int n;
double x, y, w, h, j;
int cnt;
int m;

struct Point {
	double _x, _y;
	Point(double x=0,double y=0):_x(x),_y(y){}
	bool operator <(const Point &P)
	{
		return _x < P._x || (_x == P._x&&_y < P._y);
	}
}P[maxn],ch[maxn];

typedef Point Vector;

Vector operator +(Vector p1, Vector p2) { return Vector(p1._x + p2._x, p1._y + p2._y); }
Vector operator -(Vector p1, Vector p2) { return Vector(p1._x - p2._x, p1._y - p2._y); }
//求旋转向量,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));
}
//角度换弧度
double torad(double deg)
{
	return deg / 180 * acos(-1);
}
//求两向量的叉积
double Cross(Vector A, Vector B)
{
	return A._x*B._y - A._y*B._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;
}
//计算凸包面积
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;
}

int main()
{
	ios::sync_with_stdio(false);
	cin >> T;
	while (T--)
	{
		double area1 = 0, area2 = 0;
		cnt = 0;
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			cin >> x >> y >> w >> h >> j;
			Point o(x, y);
			double angle = -torad(j);//因为j是顺时针
			P[cnt++] = o + Rotate(Vector(-w / 2, -h / 2), angle);//先旋转从中心出发的向量
			P[cnt++] = o + Rotate(Vector(w / 2, -h / 2), angle);
			P[cnt++] = o + Rotate(Vector(-w / 2, h / 2), angle);
			P[cnt++] = o + Rotate(Vector(w / 2, h / 2), angle);
			area1 += w * h;//累加木板的总面积
		}
		m = ConvexHull(P, cnt, ch);
		area2 = PolygonArea(ch, m);
		cout << setprecision(1) << fixed << area1 * 100 / area2 << " %" << endl;
	}
//	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chook_lxk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值