Areas of polygons

F.P. Laussy
fabrice.laussy@gmail.com
28 September 2024

We check and sample from Monte Carlo formulas for areas (and other properties) of irregular simple polygons.

Number of points for the sampling:

npts=(10^5);

Resolution for the binning

δA=.0025;

Return n angles on the unit circle in increasing order

Nangles[n_]:=Module[{},
Sort[Table[RandomReal[{0,2π}],{i,n}]]
]

Nangles[3]

Return their area according to the analytical formula:

NAreaTrig[θ1_,θ2_,θ3_]:=Module[{},
(1/2)(Sin[θ2-θ1]+Sin[θ3-θ2]-Sin[θ3-θ1])
]

NAreaTrig[#[[1]],#[[2]],#[[3]]]&@Nangles[3]

Checking:

Module to sample n convex points on the unit circle:

GiveMeNpts[n_]:=Module[{lθ},
{Cos [#],Sin[#]}&/@Sort[Table[RandomReal[{0,2π}],n]]
]

θfromxy[x_,y_]:=Mod[ArcTan[x,y],2π]

ex=GiveMeNpts[3]

Show[Graphics[{Triangle[ex],Circle[{0,0},1]}],ImageSize->100]

Area[Triangle[ex]]

NAreaTrig[#[[1]],#[[2]],#[[3]]]&@Sort[θfromxy[#[[1]],#[[2]]]&/@ex]

{Cos[#],Sin[#]}&/@Sort[θfromxy[#[[1]],#[[2]]]&/@ex]

Show[Graphics[{Triangle[
{Cos[#],Sin[#]}&/@Sort[θfromxy[#[[1]],#[[2]]]&/@ex]
],Circle[{0,0},1]}],ImageSize->100]

Polygons

GiveMeNpts[n_]:=Module[{lθ},
2Random[]{Cos [#],Sin[#]}&/@Sort[Table[RandomReal[{0,2π}],n]]
]

An example:

ex=GiveMeNpts[17];lθ=θfromxy[#[[1]],#[[2]]]&/@ex;
Show[Graphics[{Polygon[ex],Circle[{0,0},1],Point[{Cos[#],Sin[#]}&/@lθ],Point[{0,0}]}],
ImageSize->100]

This works only for non-intersecting (i.e., simple) polygons. This can be checked from the sum of angles (interiors, n-2, or exteriors, n+2):

(1/π)*PolygonAngle[Polygon[ex],"FullExterior"]//Total

(1/π)*PolygonAngle[Polygon[ex]]//Total

Area[Polygon[ex]]

Show[Graphics[{Polygon[ex],Circle[{0,0},1],Point[{Cos[#],Sin[#]}&/@lθ],Point[{0,0}]}],
ImageSize->100]

lθ=θfromxy[#[[1]],#[[2]]]&/@ex

lR=(Sqrt[((#[[1]])^2)+((#[[2]])^2)])&/@ex

Graphics[Polygon[Table[lR[[i]]{Cos[lθ[[i]]],Sin[lθ[[i]]]},{i,Length[lθ]}]]]

(1/2)(Sum[lR[[i]]lR[[i+1]]Sin[lθ[[i+1]]-lθ[[i]]],{i,1,Length[lθ]-1}]-lR[[1]]lR[[Length[lθ]]]Sin[lθ[[Length[lθ]]]-lθ[[1]]])

On unit circle:

GiveMeNpts[n_]:=Module[{lθ},
{Cos [#],Sin[#]}&/@Sort[Table[RandomReal[{0,2π}],n]]
]

Should return zero:

CompareAreas[n_]:=Module[{ex,lθ,lR},
ex=GiveMeNpts[n];
lθ=θfromxy[#[[1]],#[[2]]]&/@ex;
lR=(Sqrt[((#[[1]])^2)+((#[[2]])^2)])&/@ex;
Area[Polygon[ex]]-(1/2)(Sum[lR[[i]]lR[[i+1]]Sin[lθ[[i+1]]-lθ[[i]]],{i,1,Length[lθ]-1}]-lR[[1]]lR[[Length[lθ]]]Sin[lθ[[Length[lθ]]]-lθ[[1]]])
]

Timing[lt=Table[CompareAreas[11],{i,(10^5)}];]

Max[lt]

ListPlot[Sort[lt],PlotRange->All]