Shoelace formula

From HandWiki
Short description: Mathematical algorithm
Shoelace scheme for determining the area of a polygon with point coordinates [math]\displaystyle{ (x_1,y_1),...,(x_n,y_n) }[/math]

The shoelace formula, also known as Gauss's area formula and the surveyor's formula,[1] is a mathematical algorithm to determine the area of a simple polygon whose vertices are described by their Cartesian coordinates in the plane.[2] It is called the shoelace formula because of the constant cross-multiplying for the coordinates making up the polygon, like threading shoelaces.[2] It has applications in surveying and forestry,[3] among other areas.

The formula was described by Albrecht Ludwig Friedrich Meister (1724–1788) in 1769[4] and is based on the trapezoid formula which was described by Carl Friedrich Gauss and C.G.J. Jacobi.[5] The triangle form of the area formula can be considered to be a special case of Green's theorem.

The area formula can also be applied to self-overlapping polygons since the meaning of area is still clear even though self-overlapping polygons are not generally simple.[6] Furthermore, a self-overlapping polygon can have multiple "interpretations" but the Shoelace formula can be used to show that the polygon's area is the same regardless of the interpretation.[7]

The polygon area formulas

Basic idea: Any polygon edge determines the signed area of a trapezoid. All these areas sum up to the polygon area.

Given: A planar simple polygon with a positively oriented (counter clock wise) sequence of points [math]\displaystyle{ P_i=(x_i,y_i), i=1,\dots,n }[/math] in a Cartesian coordinate system.
For the simplicity of the formulas below it is convenient to set [math]\displaystyle{ P_0 = P_n, P_{n+1} = P_1 }[/math].

The formulas:
The area of the given polygon can be expressed by a variety of formulas, which are connected by simple operations (see below):
If the polygon is negatively oriented, then the result [math]\displaystyle{ A }[/math] of the formulas is negative. In any case [math]\displaystyle{ |A| }[/math] is the sought area of the polygon.[8]

Trapezoid formula

The trapezoid formula sums up a sequence of oriented areas [math]\displaystyle{ A_i=\tfrac 1 2(y_i + y_{i+1})(x_i - x_{i+1}) }[/math] of trapezoids with [math]\displaystyle{ P_iP_{i+1} }[/math] as one of its four edges (see below): [math]\displaystyle{ \begin{align} A &= \frac 1 2 \sum_{i=1}^n (y_i + y_{i+1})(x_i - x_{i+1})\\ &=\frac 1 2 \Big((y_1+y_2)(x_1-x_2)+ \cdots +(y_n+y_1)(x_n-x_1)\Big) \end{align} }[/math]

Triangle formula

The triangle formula sums up the oriented areas [math]\displaystyle{ A_i }[/math] of triangles [math]\displaystyle{ OP_iP_{i+1} }[/math]:[9] [math]\displaystyle{ \begin{align} A &= \frac 1 2 \sum_{i=1}^n (x_iy_{i+1}-x_{i+1}y_i) =\frac 1 2\sum_{i=1}^{n}\begin{vmatrix} x_i & x_{i+1} \\ y_i & y_{i+1} \end{vmatrix} =\frac 1 2\sum_{i=1}^{n}\begin{vmatrix} x_i & y_i \\ x_{i+1} & y_{i+1} \end{vmatrix}\\ &=\frac 1 2 \Big(x_1 y_2- x_2 y_1 +x_2 y_3- x_3 y_2+\cdots +x_ny_1-x_1y_n\Big) \end{align} }[/math]

Shoelace formula

Shoelace scheme, vertical form: With all the slashes drawn, the matrix loosely resembles a shoe with the laces done up, giving rise to the algorithm's name.

The triangle formula is the base of the popular shoelace formula, which is a scheme that optimizes the calculation of the sum of the 2×2-Determinants by hand: [math]\displaystyle{ \begin{align}2A &= \begin{vmatrix} x_1 & x_2 \\ y_1 & y_2 \end{vmatrix} + \begin{vmatrix} x_2 & x_3 \\ y_2 & y_3 \end{vmatrix} +\cdots + \begin{vmatrix} x_n & x_1 \\ y_n & y_1 \end{vmatrix} \\[10mu] &= \begin{vmatrix} x_1 & x_2 &x_3 \cdots &x_n&x_1\\ y_1 & y_2 &y_3 \cdots &y_n&y_1 \end{vmatrix} \end{align} }[/math]

Sometimes this determinant is transposed (written vertically, in two columns), as in the diagram.

Other formulas

[math]\displaystyle{ \begin{align} A &=\frac 1 2 \sum_{i=1}^n y_i(x_{i-1}-x_{i+1})\\ & =\frac 1 2 \Big(y_1(x_n-x_2)+y_2(x_1-x_3)+ \cdots+y_n(x_{n-1}-x_1)\Big) \end{align} }[/math] [math]\displaystyle{ A=\frac 1 2 \sum_{i=1}^n x_i(y_{i+1} - y_{i-1}) }[/math]

A particularly concise statement of the formula can be given in terms of the exterior algebra. If [math]\displaystyle{ v_1, \dots, v_n }[/math] are the consecutive vertices of the polygon (regarded as vectors in the Cartesian plane) then [math]\displaystyle{ A = \frac{1}{2} \cdot \left| \sum_{i=1}^{n} v_i \wedge v_{i+1} \right|. }[/math]

Example
Horizontal shoelace form for the example.

Example

For the area of the pentagon with [math]\displaystyle{ \begin{align} &P_1=(1,6),P_2=(3,1), P_3=(7,2),\\ &P_4=(4,4), P_5=(8,5) \end{align} }[/math] one gets [math]\displaystyle{ \begin{align} 2A &= \begin{vmatrix} 1 & 3 \\ 6 & 1 \end{vmatrix} + \begin{vmatrix} 3 & 7 \\ 1 & 2 \end{vmatrix} + \begin{vmatrix} 7 & 4 \\ 2 & 4 \end{vmatrix} + \begin{vmatrix} 4 & 8 \\ 4 & 5 \end{vmatrix} + \begin{vmatrix} 8 & 1 \\ 5 & 6 \end{vmatrix} \\ & =(1-18)\;+(6-7)\;+(28-8)\;+(20-32)\;+(48-5)=33 \\ A&= 16.5 \end{align} }[/math]

The advantage of the shoelace form: Only 6 columns have to be written for calculating the 5 determinants with 10 columns.

Deriving the formulas

Trapezoid formula

Deriving the trapezoid formula

The edge [math]\displaystyle{ P_i, P_{i+1} }[/math] determines the trapezoid [math]\displaystyle{ (x_i,y_i), (x_{i+1},y_{i+1}), (x_i,0), (x_{i+1},0) }[/math] with its oriented area

[math]\displaystyle{ A_i=\tfrac 1 2(y_i + y_{i+1})(x_i - x_{i+1}) }[/math]

In case of [math]\displaystyle{ x_i\lt x_{i+1} }[/math] the number [math]\displaystyle{ A_i }[/math] is negative, otherwise positive or [math]\displaystyle{ A_i=0 }[/math] if [math]\displaystyle{ x_i=x_{i+1} }[/math]. In the diagram the orientation of an edge is shown by an arrow. The color shows the sign of [math]\displaystyle{ A_i }[/math]: red means [math]\displaystyle{ A_i \lt 0 }[/math], green indicates [math]\displaystyle{ A_i \gt 0 }[/math]. In the first case the trapezoid is called negative in the second case positive. The negative trapezoids delete those parts of positive trapezoids, which are outside the polygon. In case of a convex polygon (in the diagram the upper example) this is obvious: The polygon area is the sum of the areas of the positive trapezoids (green edges) minus the areas of the negative trapezoids (red edges). In the non convex case one has to consider the situation more carefully (see diagram). In any case the result is [math]\displaystyle{ A = \sum_{i=1}^nA_i =\frac 1 2 \sum_{i=1}^n (y_i + y_{i+1})(x_i - x_{i+1}) }[/math]

Triangle form, determinant form

Triangle form: The color of the edges indicate, which triangle area is positive (green) and negative (red) respectively.

Eliminating the brackets and using [math]\displaystyle{ \sum_{i=1}^n x_iy_i=\sum_{i=1}^n x_{i+1}y_{i+1} }[/math] (see convention [math]\displaystyle{ P_{n+1}=P_1 }[/math] above), one gets the determinant form of the area formula: [math]\displaystyle{ A =\frac 1 2 \sum_{i=1}^n (x_i y_{i+1}-x_{i+1}y_i) =\frac 1 2\sum_{i=1}^{n}\begin{vmatrix} x_i & x_{i+1} \\ y_i & y_{i+1} \end{vmatrix} }[/math] Because one half of the i-th determinant is the oriented area of the triangle [math]\displaystyle{ O,P_i,P_{i+1} }[/math] this version of the area formula is called triangle form.

Other formulas

With [math]\displaystyle{ \sum_{i=1}^n x_iy_{i+1}=\sum_{i=1}^n x_{i-1}y_i\ }[/math] (see convention [math]\displaystyle{ P_0 = P_n, P_{n+1} = P_1 }[/math] above) one gets [math]\displaystyle{ 2A=\sum_{i=1}^n (x_iy_{i+1}-x_{i+1}y_i) =\sum_{i=1}^n x_iy_{i+1} - \sum_{i=1}^n x_{i+1}y_i =\sum_{i=1}^n x_{i-1}y_i - \sum_{i=1}^n x_{i+1}y_i }[/math] Combining both sums and excluding [math]\displaystyle{ y_i }[/math] leads to [math]\displaystyle{ A=\frac 1 2 \sum_{i=1}^n y_i(x_{i-1}-x_{i+1}) }[/math] With the identity [math]\displaystyle{ \sum_{i=1}^n x_{i+1}y_i=\sum_{i=1}^n x_iy_{i-1} }[/math] one gets [math]\displaystyle{ A=\frac 1 2 \sum_{i=1}^n x_i(y_{i+1}-y_{i-1}) }[/math]

Alternatively, this is a special case of Green's theorem with one function set to 0 and the other set to x, such that the area is the integral of xdy along the boundary.

Manipulations of a polygon

[math]\displaystyle{ A(P_1, \dots, P_n) }[/math] indicates the oriented area of the simple polygon [math]\displaystyle{ P_1, \dots, P_n }[/math] with [math]\displaystyle{ n\ge 4 }[/math] (see above). [math]\displaystyle{ A }[/math] is positive/negative if the orientation of the polygon is positive/negative. From the triangle form of the area formula or the diagram below one observes for [math]\displaystyle{ 1\lt k\lt n }[/math]: [math]\displaystyle{ A(P_1, \dots, P_n) = A(P_1, \dots, P_{k-1}, P_{k+1}, \dots, P_n) +A(P_{k-1}, P_k, P_{k+1}) }[/math] In case of [math]\displaystyle{ k=1\; \text{or}\; n }[/math] one should first shift the indices.

Hence:

  1. Moving [math]\displaystyle{ P_k }[/math] affects only [math]\displaystyle{ A(P_{k-1},P_k,P_{k+1}) }[/math] and leaves [math]\displaystyle{ A(P_1,...,P_{k-1},P_{k+1}, ...,P_n) }[/math] unchanged. There is no change of the area if [math]\displaystyle{ P_k }[/math] is moved parallel to [math]\displaystyle{ \overline{P_{k-1}P_{k+1}} }[/math].
  2. Purging [math]\displaystyle{ P_k }[/math] changes the total area by [math]\displaystyle{ A(P_{k-1},P_k,P_{k+1}) }[/math], which can be positive or negative.
  3. Inserting point [math]\displaystyle{ Q }[/math] between [math]\displaystyle{ P_k,P_{k+1} }[/math] changes the total area by [math]\displaystyle{ A(P_k,Q,P_{k+1}) }[/math], which can be positive or negative.

Example:

[math]\displaystyle{ P_1=(3,1),P_2=(7,2),P_3=(4,4), }[/math]
[math]\displaystyle{ P_4=(8,6),P_5=(1,7),\ Q=(4,3) }[/math]
Manipulations of a polygon

With the above notation of the shoelace scheme one gets for the oriented area of the

  • blue polygon: [math]\displaystyle{ A(P_1,P_2,P_3,P_4,P_5)=\tfrac 1 2 \begin{vmatrix} 3 & 7 & 4 & 8 & 1 & 3\\ 1 & 2 & 4 & 6 & 7 & 1 \end{vmatrix}= 20.5 }[/math]
  • green triangle: [math]\displaystyle{ A(P_2, P_3, P_4)=\tfrac 1 2 \begin{vmatrix} 7 & 4 & 8 & 7\\ 2 & 4 & 6 & 2 \end{vmatrix} = -7 }[/math]
  • red triangle: [math]\displaystyle{ A(P_1,Q,P_2)=\tfrac 1 2 \begin{vmatrix} 3 & 4 & 7 & 3\\ 1 & 3 & 2 & 1 \end{vmatrix} = -3.5 }[/math]
  • blue polygon minus point [math]\displaystyle{ P_3 }[/math]: [math]\displaystyle{ A(P_1, P_2, P_4, P_5)=\tfrac 1 2 \begin{vmatrix} 3 & 7 & 8 & 1 & 3\\ 1& 2 & 6 & 7 & 1 \end{vmatrix}= 27.5 }[/math]
  • blue polygon plus point [math]\displaystyle{ Q }[/math] between [math]\displaystyle{ P_1,P_2 }[/math]: [math]\displaystyle{ A(P_1, Q, P_2, P_3, P_4, P_5)=\tfrac 1 2 \begin{vmatrix} 3 & 4 & 7 & 4& 8 & 1 &3\\ 1 & 3 & 2 & 4& 6 & 7 &1 \end{vmatrix} = 17 }[/math]

One checks, that the following equations hold: [math]\displaystyle{ A(P_1, P_2, P_3, P_4, P_5) = A(P_1, P_2, P_4, P_5) + A(P_2, P_3, P_4) = 20.5 }[/math] [math]\displaystyle{ A(P_1, P_2, P_3, P_4, P_5) + A(P_1, Q, P_2) = A(P_1, Q, P_2, P_3, P_4, P_5) = 17 }[/math]

Generalization

In higher dimensions the area of a polygon can be calculated from its vertices using the exterior algebra form of the Shoelace formula (e.g. in 3d, the sum of successive cross products):[math]\displaystyle{ A = \frac{1}{2}\left\| \sum_{i=1}^{n} v_i \wedge v_{i+1} \right\| }[/math](when the vertices are not coplanar this computes the vector area enclosed by the loop, i.e. the projected area or "shadow" in the plane in which it is greatest).

This formulation can also be generalized to calculate the volume of an n-dimensional polytope from the coordinates of its vertices, or more accurately, from its hypersurface mesh.[10] For example, the volume of a 3-dimensional polyhedron can be found by triangulating its surface mesh and summing the signed volumes of the tetrahedra formed by each surface triangle and the origin:[math]\displaystyle{ V = \frac{1}{6} \left\| \sum_{F} v_a \wedge v_b \wedge v_c \right\| }[/math]where the sum is over the faces and care has to be taken to order the vertices consistently (all clockwise or anticlockwise viewed from outside the polyhedron). Alternatively, an expression in terms of the face areas and surface normals may be derived using the divergence theorem (see Polyhedron § Volume).

See also

External links

References

  1. Bart Braden (1986). "The Surveyor's Area Formula". The College Mathematics Journal 17 (4): 326–337. doi:10.2307/2686282. https://www.maa.org/sites/default/files/pdf/pubs/Calc_Articles/ma063.pdf. 
  2. 2.0 2.1 Dahlke, Karl. "Shoelace Formula". http://www.mathreference.com/la-det,shoe.html. Retrieved 9 June 2008. 
  3. Hans Pretzsch, Forest Dynamics, Growth and Yield: From Measurement to Model, Springer, 2009, ISBN 3-540-88306-1, p. 232.
  4. Meister, A. L. F. (1769), "Generalia de genesi figurarum planarum et inde pendentibus earum affectionibus" (in Latin), Nov. Com. Gött. 1: 144, https://books.google.com/books?id=fOE_AAAAcAAJ .
  5. Max Koecher, Aloys Krieg: Ebene Geometrie, Springer-Verlag, 2013,ISBN 3662068095, 9783662068090, p. 116
  6. P.W. Shor; C.J. Van Wyk (1992), "Detecting and decomposing self-overlapping curves", Comput. Geom. Theory Appl. 2 (1): 31–50, doi:10.1016/0925-7721(92)90019-O 
  7. Ralph P. Boland; Jorge Urrutia (2000). "Polygon Area Problems". 12th Canadian Conference on Computational Geometry.. pp. 159–162. 
  8. Antti Laaksonen: Guide to Competitive Programming: Learning and Improving Algorithms Through Contests, Springer, 2018, ISBN 3319725475, 9783319725475, p. 217
  9. Mauren Abreu de Souza, Humberto Remigio Gamba, Helio Pedrini: Multi-Modality Imaging: Applications and Computational Techniques, Springer, 2018,ISBN 331998974X, 9783319989747, p. 229
  10. Allgower, Eugene L.; Schmidt, Phillip H. (1986). "Computing Volumes of Polyhedra". Mathematics of Computation 46 (173): 171–174. doi:10.2307/2008221. ISSN 0025-5718. https://www.ams.org/journals/mcom/1986-46-173/S0025-5718-1986-0815838-7/S0025-5718-1986-0815838-7.pdf.