Last active
August 29, 2015 14:14
-
-
Save drewish/d5d95cdf398e5f7e7240 to your computer and use it in GitHub Desktop.
Area of a cinder::PolyLine2f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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