CP Notebook

← all snippets

PolygonArea

Returns twice the signed area of a polygon. Clockwise enumeration gives negative area. Watch out for overflow if using int as T!

6 lines Stress-tested and tested on kattis:polygonarea

Needs: "Point.h"

content/geometry/PolygonArea.h — Ulf Lundstrom, source: tinyKACTL

template<class T>
T polygonArea2(vector<Point<T>>& v) {
	T a = v.back().cross(v[0]);
	rep(i,0,sz(v)-1) a += v[i].cross(v[i+1]);
	return a;
}