| 1 | #include "polygon.cpp" |
| 2 | point 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: |
nothing calls this directly
no test coverage detected