Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!unido!gmdzi!wittig From: wittig@gmdzi.UUCP (Georg Wittig) Newsgroups: comp.graphics Subject: Re: AREA MEASUREMENT OUT OF POLYGON-CONTOUR DATA Message-ID: <3192@gmdzi.UUCP> Date: 10 Aug 90 09:58:57 GMT References: Distribution: comp.graphics Organization: GMD - The German National Research Center for Computer Science Lines: 53 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 |