描述
给一些多边形求重叠面积
分析
半平面交的模版
注意别少加了直线
#include
#include
#include
#include
using namespace std;
struct Point {
double x, y;
Point(double x=0, double y=0):x(x),y(y) { }
};
typedef Point Vector;
Vector operator + (const Vector& A, const Vector& B) { return Vector(A.x+B.x, A.y+B.y); }
Vector operator - (const Point& A, const Point& B) { return Vector(A.x-B.x, A.y-B.y); }
Vector operator * (const Vector& A, double p) { return Vector(A.x*p, A.y*p); }
double Dot(const Vector& A, const Vector& B) { return A.x*B.x + A.y*B.y; }
double Cross(const Vector& A, const Vector& B) { return A.x*B.y - A.y*B.x; }
double Length(const Vector& A) { return sqrt(Dot(A, A)); }
Vector Normal(const Vector& A) {
double L = Length(A);
return Vector(-A.y/L, A.x/L);
}
double PolygonArea(vector p) {
int n = p.size();
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;
}
// 有向直线。它的左边就是对应的半平面
struct Line {
Point P; // 直线上任意一点
Vector v; // 方向向量
double ang; // 极角,即从x正半轴旋转到向量v所需要的角(弧度)
Line() {}
Line(Point P, Vector v):P(P),v(v) {
ang = atan2(v.y, v.x);
}
bool operator < (const Line& L) const {
return ang < L.ang;
}
};
// 点p在有向直线L的左边(线上不算)
bool OnLeft(const Line& L, const Point& p) {
return Cross(L.v, p-L.P) > 0;
}
// 二直线交点,假定交点惟一存在
Point GetLineIntersection(const Line& a, const Line& b) {
Vector u = a.P-b.P;
double t = Cross(b.v, u) / Cross(a.v, b.v);
return a.P+a.v*t;
}
const double INF = 1e8;
const double eps = 1e-6;
// 半平面交主过程
vector HalfplaneIntersection(vector
本文介绍如何使用模板计算多个多边形的重叠面积,包括直线操作、半平面交集等关键步骤。
436

被折叠的 条评论
为什么被折叠?



