Area of a Spline Polygon — With C code Sample
©2009 Darel Rex Finley.  This complete article, unmodified, may be freely distributed for educational purposes.

On this page we learned how to make a polygon with spline curves.  But what if you want to know the area of such a polygon?  Impossibly difficult?  Actually, it’s pretty easy.

0,17,  4,24,  26,22,  SPLINE,20,2,  7,1,  5,5,  10,10,  SPLINE,9,16,  END
Figure 1

In Figure 1 we see a spline polygon.  It has 8 corners, 2 of which are spline corners.

0,17,  4,24,  26,22,  7,1,  5,5,  10,10,  END
Figure 2

First, we ignore the spline corners, and calculate the area of the yellow polygon in Figure 2, which is composed of all the hard corners of our spline polygon.  We can do that as described in this page.

26,22,  20,2,  7,1,  END
10,10,  9,16,  0,17,  END
Figure 3

Next, using the same polygon-area method, calculate the area of the triangles that surround each spline, as illustrated in Figure 3.  (Note that the grey triangle will automatically give negative area, since its enclosing triangle is counter-clockwise.  That’s fine — it’s what we want.)

Figure 4

Now, simply multiply each of the triangle areas by the fraction of the triangle that is covered by its spline curve — that gives you the area enclosed by each spline curve, as shown in Figure 4.  Then it is a simple matter to add these areas to the area of the original yellow polygon...

Figure 5

And there it is.  The area of the spline polygon.  Now wasn’t that easy?

Oh wait, I almost forgot.  How do we know what fraction of each triangle is covered by its spline curve?  Simple.  It’s 2/3.  Every time.  That’s all you need to know to get the area — but if you really want to know where the 2/3 comes from ... here goes:

Figure 6

Figure 6 shows a 3-point spline that is conveniently nestled inbetween the X and Y axes in a one-unit square.  The curve consists of all points x,y that correspond to a fraction F, such that if you draw a line segment from the Y axis to the X axis, that starts F down the Y axis, and ends F across the X axis, then x,y lies on that segment, and is exactly F of the way from the segment’s start to its end.

Because this definition is entirely based on straight line segments and linear proportions (i.e. F) along those line segments, it follows that the fraction of the triangle occupied by this 3-point spline curve will be the same for any 3-point spline curve — i.e. moving the three points around will linearly remap the spline curve to the new triangle, and so the percentage area covered by it will be the same.  So if we can figure it out for this 3-point spline curve, we’ve figured it out for all of them.

Along the X axis, x is F of the way to F, so:

     x  =  F2

And by the same logic:

     y  =  (1-F)2

Expand to:

     y  =  1 - 2F + F2

Since x is F2, we can get rid of F, like so:

     y  =  1 - 2x0.5 + x

Now integrate to get the area formula:

     area   =   x  -  (4/3)x1.5  +  (1/2)x2

Figure 7

The area formula gives us the area enclosed by the curve on the top, the X axis on the bottom, the Y axis on the left, and x on the right.  So we can simply plug in 1 for x, and the formula will give us the area (yellow in Figure 7) of the triangle that is outside the convex spline curve:

     area   =   1  -  (4/3)11.5  +  (1/2)12

     area   =   1  -  4/3  +  1/2

     area   =   1/6

Figure 8

We can plainly see that the area of the triangle is 1/2, so the part of the triangle’s area that is inside the convex spline curve (yellow in Figure 8) is:

     area inside spline   =   1/2  -  1/6

     area inside spline   =   1/3

Divide this area by the area of the whole triangle to get the fraction of the triangle that it covers:

     fraction of triangle covered    =    1/3   /   1/2

     fraction of triangle covered    =    2/3

And there it is.

C Example Code:

//  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; }

Send me an e-mail!

Back to tutorials.