# How to get properties from a Polygon object?

We can get polygons by this code:

polys = (SeedRandom[#]; Polygon[RandomInteger[20, {4, 2}]]) & /@ Range[6]


How to get those properties? When I dig GeneralUtilitiesPrintDefinitions[Polygon], I know

• "Number of points"

RegionPolygonDumppolygonCoordinateCount[#] &[polys[[1]]]

4

• Embedding dimension

RegionPolygonDumppolygonEmbeddingDimension[#] &[polys[[1]]]

2

• "Type"

RegionPolygonDumppolygonType@@RegionPolygonDumpcomputeType[polys[[1]]]

"degenerated polygon"

• Bounds

RegionPolygonDumpiRegionBounds[polys[[1]]]

{{0, 7}, {0, 3}}

• Area

RegionPolygonDumpiArea[polys[[1]]]

undefined

But it seem too dirty. Is there any mma-style method to make this?

• p=((SeedRandom[#]; Polygon[RandomInteger[20, {4, 2}]]) & /@ Range[1])[[1]]; FullForm[p] shows me that Length[p[[1]]] tells me the number of points.
– Bill
Commented Mar 26 at 3:36
• @Bill, this will work correctly only for non-disconnected polygons. It will not work for something like pDis = Polygon[{{{1, 1}, {1, 2}, {2, 1}}, {{5, 5}, {5, 6}, {7, 8}}}]. The simple way is to just use the internal function: With[{coords = pDis[[1]]}, RegionPolygonDumpcoordinateCount[coords]]. Commented Mar 26 at 9:11
• A look at the output of MakeBoxes[#, StandardForm] &@Polygon[RandomInteger[20, {4, 2}]] shows many of them are computed on the fly by internal functions, such as the one pointed out by @Domen. The code for these function, at least the couple I checked, can be inspected. Commented Mar 26 at 11:19

\$Version

(* "14.0.0 for Mac OS X ARM (64-bit) (December 13, 2023)" *)

Clear["Global*"]

SeedRandom[1234];

poly = RandomPolygon[4]


For a given polygon:

coords = PolygonCoordinates[poly]

(* {{0.0116446, 0.927266}, {0.0862234, 0.377913}, {0.543757,
0.479332}, {0.876608, 0.521964}} *)

nbrPts = Length[coords]

(* 4 *)

SimplePolygonQ[poly]

(* True *)

bounds = MinMax /@ Transpose[coords]

(* {{0.0116446, 0.876608}, {0.377913, 0.927266}} *)

area = Area[poly]

(* 0.0930163 *)

Perimeter[poly]

(* 2.92281 *)

• PolygonCoordinates[polys[[3]]] will get 5 points. Also polys[[1]]
– yode
Commented Mar 26 at 5:27
• @yode - If the polygons are to be "well-behaved" they must be simple polygons. To get simple polygons, use RandomPolygon. Compare Length[Select[SeedRandom[1234]; Polygon /@ RandomInteger[20, {100, 4, 2}], SimplePolygonQ]] (53) with Length[Select[SeedRandom[1234]; RandomPolygon[20, 100], SimplePolygonQ]] (100) Commented Mar 26 at 13:17