LA 2402 多边形切割

本文详细阐述了一种基于几何图形的切割算法实现过程,通过预处理数据和一系列数学运算,有效切割并计算不同形状的几何区域面积,适用于复杂图形处理场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <bits/stdc++.h>
using namespace std;
const int maxn = 35;
int n;
double a[maxn], b[maxn], c[maxn], d[maxn];
struct Point
{
	double x, y;
	Point(double x = 0, double y = 0): x(x), y(y) {}
};
typedef Point Vector;
typedef vector<Point> Polygon;
Vector operator +(Vector A, Vector 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);
}
bool operator <(const Point &a, const Point &b)//
{
	return a.x < b.x || (a.x == b.x && a.y < b.y);
}
const double eps = 1e-10;
int dcmp(double x)//
{
	if (fabs(x) < eps) return 0;
	else return x < 0 ? -1 : 1;
}
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;
}
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 OnSegment(Point p, Point a1, Point a2) //
{
	return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0;
}
double PolygonArea(Polygon &p) //
{
	double area = 0;
	for (int i = 1; i < p.size() - 1; i++)
		area += Cross(p[i] - p[0], p[i + 1] - p[0]);
	return area / 2;
}
Polygon CutPolygon(Polygon poly, Point A, Point B)
{
	Polygon newpoly;
	int n = poly.size();
	for (int i = 0; i < n; i++)
	{
		Point C = poly[i];
		Point D = poly[(i + 1) % n];
		if (dcmp(Cross(B - A, C - A)) >= 0) newpoly.push_back(C);
		if (dcmp(Cross(B - A, C - D)) != 0)
		{
			Point ip = GetLineIntersection(A, B - A, C, D - C);
			if (OnSegment(ip, C, D)) newpoly.push_back(ip);
		}
	}
	return newpoly;
}
vector<Polygon>pieces;
int main(int argc, char const *argv[])
{
	while (~scanf("%d", &n) && n)
	{
		pieces.clear();
		Polygon poly;
		poly.push_back(Point(0, 0));
		poly.push_back(Point(1, 0));
		poly.push_back(Point(1, 1));
		poly.push_back(Point(0, 1));
		pieces.push_back(poly);
		for (int i = 0; i < n; i++)
			scanf("%lf", &a[i]);
		for (int i = 0; i < n; i++)
			scanf("%lf", &b[i]);
		for (int i = 0; i < n; i++)
			scanf("%lf", &c[i]);
		for (int i = 0; i < n; i++)
			scanf("%lf", &d[i]);
		for (int i = 0; i < n; i++)
		{
			vector<Polygon> temp1, temp2;
			for (int j = 0; j < pieces.size(); j++)
			{
				Polygon poly1 = CutPolygon(pieces[j], Point(a[i], 0), Point(b[i], 1));
				Polygon poly2 = CutPolygon(pieces[j], Point(b[i], 1), Point(a[i], 0));
				if (poly1.size() >= 3)
					temp1.push_back(poly1);
				if (poly2.size() >= 3)
					temp1.push_back(poly2);
			}
			pieces = temp1;
			for (int j = 0; j < pieces.size(); j++)
			{
				Polygon poly3 = CutPolygon(pieces[j], Point(0, c[i]), Point(1, d[i]));
				Polygon poly4 = CutPolygon(pieces[j], Point(1, d[i]), Point(0, c[i]));
				if (poly3.size() >= 3)
					temp2.push_back(poly3);
				if (poly4.size() >= 3)
					temp2.push_back(poly4);
			}
			pieces = temp2;
		}
		double maxx = -1;
		for (int i = 0; i < pieces.size(); i++)
			maxx = max(maxx, PolygonArea(pieces[i]));
		printf("%lf\n", maxx);
	}
	return 0;
}



熟练运用书上模板,暴力切割就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值