Finding the Area of an Arbitrary Shape

hello,

i am working on an app where i have a shape defined by:

ofSetColor(spacePeriRGB[0], spacePeriRGB[1], spacePeriRGB[2]);
ofNoFill;
ofSetPolyMode(OF_POLY_WINDING_ODD);
ofBeginShape();
for(int i = 0; i < pointCt; i++){
ofVertex(spacePts[i][0], spacePts[i][1]);
}
ofEndShape(true);

i use the for-loop b/c the spacePts array is dynamic (by using a double pointer and initializing each 2d array which hold the individual x,y values with the new operator). checks are put in place so that there will always be at least 3 points.

the points locations are defined by the user by way of the x, y parameters of the mousePressed() function thus making the locations of these control points arbitrary.

now, i need the area of the space created by the above shape definition. again, the space is arbitrary so trying to implement some analytic method using some advanced mathematical techniques will be difficult and unpleasant.

does anyone know how i can get the area of the space created?

the only way i can think of is by filling the space with some color, then getPixels() and finally scan through the returned unsigned char array and compare to the background color. So it will look something like:

ofImage myImage;
myImage.grabScreen(0,0,ofGetWidth(),ofGetHeight());
myImage.setImageType(OF_IMAGE_COLOR);//to get RGB values
unsigned char *myPixels = myImage.getPixels();
int area = 0;
for(int i = 0; i < ofGetWidth() * ofGetHeight() - 1; i + 3){
//assume the pixel array is an RGB array
if(myPixels[i]=bgRGB[0] && myPixels[i+1]=bgRGB[1] && myPixels[i+2]=bgRGB[2]){
area++;
}
}

is there some other way available in openframeworks or some other library? the above method seems wasteful.

any suggestions will be appreciated…thanks!


Best!

as usual, Paul Bourke has the answer : )

http://paulbourke.net/geometry/polyarea/

there’s also c code so it should be easy to implement

thanks for the reply!! and paul bourke’s whole site is great, i’m sure i’ll be on there often.

Now let me be a real pain , does anyone know how to get the area of an arbitrary closed beizer curve?

this is just a itch, but any replies would be appreciated. thnks

i guess you can approximate it by decomposing the bezier into a polygon and then finding the area for that.

again in Paul Bourke’s page : )

http://paulbourke.net/geometry/bezier/index2.html

it’s also in OF code, take a look at ofGraphics.cpp