polyarea

Area of a polygon given by its vertex coordinates.

Category: Mathematics > Computational Geometry

Syntax

A = polyarea(x, y)

Area of the polygon with vertices (x, y).

A = polyarea(x, y, dim)

For matrices, treats the polygons as running along dimension dim.

Description

The area follows from the shoelace formula, a sum of cross products of consecutive vertices. The polygon is closed automatically, so the first vertex need not be repeated. The result is the absolute value, so clockwise and counterclockwise lists give the same number.

For a self-intersecting polygon, regions traversed in opposite directions subtract from each other. Use polyshape for robust geometry with holes.

Input arguments

x, y
Vectors of vertex coordinates in order around the polygon, or matrices with one polygon per column.
dim
Dimension along which the vertices run.

Output arguments

A
Non-negative area. For matrix input, a row vector with one area per column.

Example

A square and two rectangles

polyarea([0 1 1 0], [0 0 1 1])
X = [0 1 1 0; 0 2 2 0]';
Y = [0 0 1 1; 0 0 3 3]';
polyarea(X, Y)
Output
ans = 1
ans = 1×2

     1     6

See also

area polyshape inpolygon convhull trapz