Numerical experiments, Tips, Tricks and Gotchas

Numerically speaking

Area calculation

How to calculate the area inside a closed curve


According to Green's theorem [1] the area bounded by a closed curve $C$ can be expressed as a contour (path) integral around this curve:

Green's theorem

Any of the above three definitions can be used.

Polygon area

A polygon is a closed plane figure bounded by straight sides. Polygons include many common figures (triangles, rectangles, etc.). They also can be used for approximation of more complex shapes. Below is an irregular octagon:

Polygon area

Fig. 1. Polygon area.

For polygons the total area is calculated as \[ A=\sum_{i=1}^{N-1}I_{i+1,i}+I_{1,N} \] where e.g. \[ I_{2,1}=\intop_{x_{1}}^{x_{2}}ydx=\frac{1}{2}(y_{2}+y_{1})(x_{2}-x_{1}) \]

Note that changing of contour path direction changes the integral sign. Because of this the calculated area $ A = 0 $ for the self-intersecting polygon in Fig. 2a (two equal triangles with the opposite path directions).

Polygon area Polygon area
Fig. 2a. Self-intersecting polygon; A = 0. Fig. 2b. Irregular hexagon; A = 3.

However for the polygon in Fig. 2b the calculated area $ A = 3 $ .


Polygon perimeter

Perimeter calculation is trivial: \[ P=\sum_{i=1}^{N-1}L_{i+1,i}+L_{1,N} \] where e.g. \[ L_{2,1}=\sqrt{(y_{2}-y_{1})^{2}+(x_{2}-x_{1})^{2}} \] Opviously $P$ is always positive.


Numerical experiments

The formulae above were implemented in Python. The IPython HTML notebook experiments are available at the links below.

The IPython HTML notebook can be:



  1. Wikipedia, Green's theorem.


© Nikolai Shokhirev, 2012-2017

email: nikolai(dot)shokhirev(at)gmail(dot)com