File Exchange

## Closed area calculation

version 1.0 (7.35 KB) by

Computes the area inside a closed curve.

Updated

This function calculates the area enclosed by a closed curve C, which can be traveled 'counterclockwise' ONLY, not crossed loops.
It is a simplified version of the Matlab's 'polyarea' function. Which has no restriction on the orientation of the polygon plus other abilities. However it's 40% faster than the matlab version due to its simplicity.
Coordinate points (x,y) are given by the matrix 'C', where: col.1 = x_coords. and col.2 = y_coords.
The calculation method it is based on the Green's Theorem.

serdar

### serdar (view profile)

John D'Errico

Some additional testing shows this code does offer a moderate speed incentive over polyarea, IFF your polygons are relatively small, and you know them to be oriented in the proper direction. For example, large polygons are by far best served with the existing polyarea:

n = 10000;
theta = linspace(0,2*pi,n)';
xy = [cos(theta),sin(theta)];

tic,a = f_closed_area(xy);toc
Elapsed time is 1.863020 seconds.

tic,a = polyarea(xy(:,1),xy(:,2));toc
Elapsed time is 0.014050 seconds.

However, small ones can gain with f_closed_area:

n = 10;
theta = linspace(0,2*pi,n)';
xy = [cos(theta),sin(theta)];
tic,for i = 1:1000,a = f_closed_area(xy);end,toc
Elapsed time is 0.126305 seconds.

tic,for i = 1:1000,a = polyarea(xy(:,1),xy(:,2));end,toc
Elapsed time is 0.412061 seconds.

Those of an intermediate size are equally well served by both codes.

n = 100;
theta = linspace(0,2*pi,n)';
xy = [cos(theta),sin(theta)];

tic,for i = 1:1000,a = f_closed_area(xy);end,toc
Elapsed time is 0.653568 seconds.

tic,for i = 1:1000,a = polyarea(xy(:,1),xy(:,2));end,toc
Elapsed time is 0.457729 seconds.

In either code I see no problems with the results in a numerical sense. So, if you are in the need for speed...

John D'Errico

Why would you download a restrictive code (only counterclockwise polygons) to compute polygon area when a better code already exists in every copy of matlab? Matlab's polyarea has no restriction on orientation of the polygon, plus more abilities than this code.

help polyarea