// public domain function by Darel Rex Finley, 2009
double splinePolyArea(double *poly) {
#define SPLINE 9999. // These constants must be well outside
#define END -9999. // the range of your polygon.
double area=0., a, b, Sx, Sy, Ex, Ey ;
int i=0, j, k ;
while (poly[i]!=END) {
j=i+2; if (poly[i]==SPLINE) j++;
if (poly[j]==END) j=0;
if (poly[i]!=SPLINE && poly[j]!=SPLINE) { // STRAIGHT LINE
area+=(poly[i]+poly[j])*(poly[i+1]-poly[j-1]); }
else if (poly[j]==SPLINE) { // SPLINE CURVE
a=poly[j+1]; b=poly[j+2]; k=j+3; if (poly[k]==END) k=0;
if (poly[i]!=SPLINE) {
Sx=poly[i]; Sy=poly[i+1]; }
else { // interpolate hard corner
Sx=(poly[i+1]+poly[j+1])/2.; Sy=(poly[i+2]+poly[j+2])/2.; }
if (poly[k]!=SPLINE) {
Ex=poly[k]; Ey=poly[k+1]; }
else { // interpolate hard corner
Ex=(poly[j+1]+poly[k+1])/2.; Ey=(poly[j+2]+poly[k+2])/2.; }
area+= (Sx+Ex)*(Sy-Ey);
area+=((Sx+a )*(Sy-b )+(a+Ex)*(b-Ey)+(Ex+Sx)*(Ey-Sy))*(2./3.); }
if (poly[i]==SPLINE) i++;
i+=2; }
return area*.5; }
|