## Finding the area of an irregular polygon

Super Contributor
Posts: 259

# Finding the area of an irregular polygon

I'm trying to find the area for an irregular polygon. My dataset consists of many points but the example below contains just 8 coordinates.

Is there a good algorithm available for doing this? Ideally generalizing it so that it can apply to potentially 100s of points.

data coord;

input x 1. y 1.;

datalines;

14

31

53

87

79

58

47

25

run;

SAS Super FREQ
Posts: 3,841

## Re: Finding the area of an irregular polygon

These computational geometry questions might be better to ask in the SAS Support Community for Graphics, which includes experts in mapping and GIS. Goto https://communities.sas.com/community/support-communities/sas_graph_and_ods_graphics They might know of a built-in function or macro.

These computations are possible to do in the DATA step, but they are much easier in a vector language like SAS/IML.

A simple internet search will reveal many web pages that discuss formulas for the area of a general polygon. For example, see Area of Triangles and Polygons. The following SAS/IML function implements the second summation formula after the paragraph that begins "One can make the formula more explicit by..."

proc iml;
start AreaPolygon(_x, _y);
/* close polygon by appending first point to end */
y = _y // _y[1];
x = _x // _x[1];
n = nrow(x);
xx = x[ 1n-1) ] + x[ 2:n ];
yy = y[ 2:n ] - y[ 1n-1) ];
A = 0.5 * sum(xx # yy); /* is negative for clockwise polys */
return( abs(A) );
finish;

use coord; read all var {x y}; close;
A = AreaPolygon(x,y);
print A;

Super Contributor
Posts: 301

## Re: Finding the area of an irregular polygon

Or try this

data elipse;

pi=4*atan(1);

do i=0 to 256 ;

x=cos(i*2*pi/256);

y=2*sin(i*2*pi/256);

output;

end;

run;

data _NULL_;

set elipse end=eof;

retain area lastx lasty;

if _N_=1 then area=0;

else do;

area+((lastx-x)*(y+lasty)/2);

end;

lastx=x;lasty=y;

if eof then put area=;

run;

Discussion stats
• 2 replies
• 311 views
• 0 likes
• 3 in conversation