Polygon Area¶
Shoelace formula¶
Vertices \((x_0,y_0), \ldots, (x_{n-1},y_{n-1})\) in order along the boundary (counterclockwise gives positive signed area):
\[
A = \frac{1}{2} \left| \sum_{i=0}^{n-1} (x_i y_{i+1} - x_{i+1} y_i) \right|
\]
with \((x_n, y_n) = (x_0, y_0)\).
#include <vector>
#include <cmath>
struct Point { double x, y; };
// Signed area: positive if CCW, negative if CW
double polygonAreaSigned(const std::vector<Point>& p) {
int n = (int)p.size();
double s = 0;
for (int i = 0; i < n; i++) {
int j = (i + 1) % n;
s += p[i].x * p[j].y - p[j].x * p[i].y;
}
return s / 2.0;
}
double polygonAreaAbs(const std::vector<Point>& p) {
return std::fabs(polygonAreaSigned(p));
}
Pick’s theorem (integer grid)¶
For a simple polygon with vertices on integer coordinates:
\[
A = I + \frac{B}{2} - 1
\]
where \(I\) = interior lattice points, \(B\) = boundary lattice points.
Related¶
- Points & Lines — orientation
- Convex Hull — order of hull vertices