Skip to content

Instantly share code, notes, and snippets.

@drewish
Last active August 29, 2015 14:14
Show Gist options
  • Save drewish/d5d95cdf398e5f7e7240 to your computer and use it in GitHub Desktop.
Save drewish/d5d95cdf398e5f7e7240 to your computer and use it in GitHub Desktop.
Area of a cinder::PolyLine2f
// Assumes line does not self intersecting
// Treats the line as closed (you wanted area right?)
// http://www.mathsisfun.com/geometry/area-irregular-polygons.html
float area( PolyLine2f line ) {
float sum = 0.0;
if ( line.size() > 1 ) {
PolyLine2f::iterator it;
Vec2f prev, curr;
for ( it = line.begin(), prev = *it++; it != line.end() ; it++ ) {
curr = *it;
sum += ( prev.y + curr.y ) / 2.0 * ( curr.x - prev.x );
prev = curr;
}
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment