db cir()
计算多边形周长
db dir_area()
计算多边形有向面积
db area()
计算多边形面积
void fix()
如果点是以顺时针顺序存储,则改为逆时针
int poly::point_in(pt t)
判断点和多边形的关系:0 在外面,1 在内部,2 在边上
int poly::Border_pt_num()
计算多边形边上的整点个数
int poly::Inside_pt_num()
计算多边形内部点的个数
pt poly::MassCenter()
计算多边形的重心
namespace Poly {
struct poly {
vector<pt> a;
poly(int sz = 0) {
a.resize(sz);
}
db cir() {
db res = 0; int n = (int)a.size();
for (int i = 0; i < n; i++) res += dist(a[i], a[nex(i)]);
return res;
}
db dir_area() {
db sum = 0; int n = (int)a.size();
for (int i = 0; i < n; i++) sum += det(a[i], a[nex(i)]);
return (sum / 2.0);
}
db area() {
return fabs(dir_area());
}
void fix() {
if (cmp(dir_area()) < 0) reverse(a.begin(), a.end());
}
int point_in(pt t);
int Border_pt_num();
int Inside_pt_num();
pt MassCenter();
};
int poly::point_in(pt t) {
int num = 0, n = (int)a.size(), d1, d2, k;
for (int i = 0; i < n; i++) {
if (Line::pt_on_seg(t, a[i], a[nex(i)])) return 2;
k = cmp(det(a[nex(i)] - a[i], t - a[i]));
d1 = cmp(a[i].y - t.y);
d2 = cmp(a[nex(i)].y - t.y);
if (k > 0 && d1 <= 0 && d2 > 0) num++;
if (k < 0 && d2 <= 0 && d1 > 0) num--;
}
return num != 0;
}
int poly::Border_pt_num() {
int num = 0, n = (int)a.size();
for (int i = 0; i < n; i++) {
int xx = int(fabs(a[nex(i)].x - a[i].x) + 0.01);
int yy = int(fabs(a[nex(i)].y - a[i].y) + 0.01);
num += gcd(xx, yy);
}
return num;
}
int poly::Inside_pt_num() {
return int(area() + 1 - Border_pt_num() / 2);
}
pt poly::MassCenter() {
int n = (int)a.size();
pt ans = pt(0, 0);
if(cmp(area()) == 0) return ans;
for(int i = 0; i < n; i++)
ans = ans + (a[i] + a[nex(i)]) * det(a[i], a[nex(i)]);
return ans / dir_area() / 6.0;
}
}
using namespace Poly;