[comp.graphics] AREA MEASUREMENT OUT OF POLYGON-CONTOUR DATA

dick@prisma.cv.ruu.nl (Dick Bakker) (08/09/90)

I am looking for a method to calculate the area of a 
contour out of the polygon-data that describes the 
contour.

e.g. I have 100 points that describe a contour (x,y-
coordinates) and I'd like to calculate the area of it.

Does anyone know an algorithm that can do such a thing?


Thanks in advance,

				Dick Bakker 

tevfik@ulrik.uio.no (Tevfik Karagulle) (08/10/90)

In article <dick.650211355@prisma> dick@prisma.cv.ruu.nl (Dick Bakker) writes:

>>   I am looking for a method to calculate the area of a 
>>   contour out of the polygon-data that describes the 
>>   contour.

>>   e.g. I have 100 points that describe a contour (x,y-
>>   coordinates) and I'd like to calculate the area of it.

>>   Does anyone know an algorithm that can do such a thing?

From 'Foley, Van Dam : Fundamentals of Interactive Computer Graphics, 1982,
	p.513' :

	For instance, the area C of the polygon projected onto xy plane is just

	          n
	         ----
	      1  \
      	C =   -   \     (y  +  y  )  (x  +  x)
	      2   /       i     i@1    i     i@1
                 /
                  ----
	          i=1

	where the operator @ is normal addition except that n@1 = 1.


Regards,

Tevfik Karagulle, UiO, Norway  (Internet : tevfik%vision01@ifi.uio.no)

tevfik@ulrik.uio.no (Tevfik Karagulle) (08/10/90)

NB! There is a mistake in my previous posting. Please read this one :

   From 'Foley, Van Dam : Fundamentals of Interactive Computer Graphics, 1982,
	   p.513' :


   For instance, the area C of the polygon projected onto xy plane is just

		     n
		    ----
		 1  \
	   C =   -   \     (y  +  y  )  (x  +  x)
		 2   /       i     i@1    i ^    i@1
		    /                       |
		     ----                   |
		     i=1              NB! THIS MUST BE - (minus).

	   where the operator @ is normal addition except that n@1 = 1.


   Regards,

   Tevfik Karagulle, UiO, Norway  (Internet : tevfik%vision01@ifi.uio.no)

wittig@gmdzi.UUCP (Georg Wittig) (08/10/90)

dick@prisma.cv.ruu.nl (Dick Bakker) writes:

>I am looking for a method to calculate the area of a 
>contour out of the polygon-data that describes the 
>contour.

>e.g. I have 100 points that describe a contour (x,y-
>coordinates) and I'd like to calculate the area of it.

>Does anyone know an algorithm that can do such a thing?

>Thanks in advance,
>				Dick Bakker 

Let the polygon be the sequence {(X[i], Y[i]) | i = 0 ... N-1}
For each I in 0 ... N-2 there exists a trapezium with the 4 corners
	(X[I  ], 0     ),
	(X[I+1], 0     ),
	(X[I+1], Y[I+1]),
	(X[I  ], Y[I  ])
The area of it is	( X[I+1] - X[I] ) * ( Y[I] + Y[I+1] ) / 2
(i.e., height * length of the center line).

So, if your points are stored as floating point, your program could be

	double area, *px, *py;
	area = 0.0;					/* if polygon is open */
--or--	area = (X[N-1] - X[0]) * (Y[N-1] + Y[0] );	/* if it is closed */

	for (px = &X[N-2], py = &Y[N-2];   px >= X;   --px, --py)
		area += ( px[0] - px[1] ) * ( py[0] + py[1] );
	area *= 0.5;

If your coordinates are integers, then it is wise, not to divide by 2
because of the rounding errors. Just store the value that isn't divided by
2; so you avoid rounding errors. When the value of the area is printed to
the user, it can be divided by 2 in the print statement:

	long area, *px, *py;
	area=0L;					/* if polygon open */
--or--	area = (X[N-1] - X[0]) * (Y[N-1] + Y[0] );	/* if pol. closed */

	for (px = &X[N-2], py = &Y[N-2];   px >= X;   --px, --py)
		area += ( px[0] - px[1] ) * ( py[0] + py[1] );
	...
	printf ("area is %ld\n", area/2);


-- 
Georg Wittig  GMD-Z1.IT	| wittig@gmdzi.gmd.de	| "Freedom's just another word
P.O. Box 1240		| wittig@zi.gmd.dbp.de	|  for nothing left to lose"
D-5205 St. Augustin 1	|			| (Kris Kristofferson)
West Germany		| (+49) 2241 14-2294	|