MCPcopy Create free account
hub / github.com/SuprDewd/CompetitiveProgramming / polygon_centroid

Function polygon_centroid

code/geometry/polygon_centroid.cpp:2–16  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1#include "polygon.cpp"
2point polygon_centroid(polygon p) {
3 double cx = 0.0, cy = 0.0;
4 double mnx = 0.0, mny = 0.0;
5 int n = size(p);
6 rep(i,0,n)
7 mnx = min(mnx, real(p[i])),
8 mny = min(mny, imag(p[i]));
9 rep(i,0,n)
10 p[i] = point(real(p[i]) - mnx, imag(p[i]) - mny);
11 rep(i,0,n) {
12 int j = (i + 1) % n;
13 cx += (real(p[i]) + real(p[j])) * cross(p[i], p[j]);
14 cy += (imag(p[i]) + imag(p[j])) * cross(p[i], p[j]); }
15 return point(cx, cy) / 6.0 / polygon_area_signed(p)
16 + point(mnx, mny); }
17// vim: cc=60 ts=2 sts=2 sw=2:

Callers

nothing calls this directly

Calls 3

crossFunction · 0.85
polygon_area_signedFunction · 0.85
pointClass · 0.70

Tested by

no test coverage detected