Function Repository Resource:

SignedArea

Source Notebook

Compute the signed area of a polygon

Contributed by: Ed Pegg Jr and Jan Mangaldan

ResourceFunction["SignedArea"][pts]

computes the signed area of a polygon built out of pts in the given order.

Details

Typically, the signed area of counterclockwise oriented points is positive, while the area for clockwise oriented points is negative.
Often in computer graphics, an outer polygon has counterclockwise points, but any interior hole has clockwise points. The area of the polygon with holes is thus the sum of the signed areas.
ResourceFunction["SignedArea"][Polygon[pts]] is equivalent to ResourceFunction["SignedArea"][pts].

Examples

Basic Examples (2) 

Calculate the signed area of a square:

In[1]:=
ResourceFunction["SignedArea"][{{0, 0}, {1, 0}, {1, 1}, {0, 1}}]
Out[1]=

Calculate the area of a set of points making a simple polygon:

In[2]:=
simple = {{12, 2}, {22, 2}, {23, 11}, {17, 16}, {28, 27}, {12, 30}, {4, 20}, {4, 9}};
ResourceFunction["SignedArea"][simple]
Out[3]=

Show the counterclockwise nature of the original points:

In[4]:=
Graphics[{EdgeForm[Black], White, Polygon[simple], Black, Table[Style[Text[n, simple[[n]]], 14], {n, 1, 8}]}]
Out[4]=

Rotating the points does not change the signed area:

In[5]:=
ResourceFunction["SignedArea"][RotateRight[simple, 4]]
Out[5]=

Reversing the order of the points reverses the sign of the area:

In[6]:=
ResourceFunction["SignedArea"][Reverse[simple]]
Out[6]=

Swapping the coordinates also makes the signed area negative:

In[7]:=
ResourceFunction["SignedArea"][Reverse /@ simple]
Out[7]=

Scope (3) 

Find the signed area of a Polygon:

In[8]:=
ResourceFunction["SignedArea"][
 Polygon[{{1, 0}, {0, Sqrt[3]}, {-1, 0}}]]
Out[8]=

Find the signed area of a regular polygon:

In[9]:=
ResourceFunction["SignedArea"][RegularPolygon[5]] // Simplify
Out[9]=

Create ten simple random polygons:

In[10]:=
tours = Table[
   With[{p = RandomReal[{0, 9}, {10, 2}]}, p[[Last[FindShortestTour[p]]]]], {10}];

Calculate the signed areas:

In[11]:=
ResourceFunction["SignedArea"] /@ tours // Timing
Out[11]=

Use the Area function instead. Note the increased time:

In[12]:=
Area[Polygon[#]] & /@ tours // Timing
Out[12]=

Use SignedArea to find the winding numbers:

In[13]:=
Sign[ResourceFunction["SignedArea"] /@ tours]
Out[13]=

Polygons from RandomPolygon have a positive signed area:

In[14]:=
ResourceFunction["SignedArea"][First[#]] & /@ Table[RandomPolygon[9], {8}]
Out[14]=

Run a timing test between SignedArea and Area:

In[15]:=
poly = Table[RandomPolygon[100], {1000}];
In[16]:=
Short[Timing[ResourceFunction["SignedArea"][First[#]] & /@ poly]]
Out[16]=
In[17]:=
Short[Timing[Area[#] & /@ poly]]
Out[17]=

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 01 September 2023

Related Resources

License Information