# Polygons crash kernel?

Having some problems Generating random Polygons and then filtering by Area, specifically filter out degenerated polygons with undefined Area is crashing the kernel in 12.0.

randomThirdPoints[n_] := Table[1/3 {RandomChoice[{0,1,2,3}],RandomChoice[{0,1,2,3}]},{n}]


Here are some random 4-gons in the unit square:

randomPolygons4 := Table[Polygon[randomThirdPoints[4]], 100];


This step is consistently crashing kernel:

randomPolygons4 // Map[Area]


similarly if using Select, AssociationMap and also using RegionQ instead of Area.

Are there some safer options? Ref Page doesn't seem to help.

• It's not the mapping that's the problem, it's just that the Area function crashes for some polygons. You SeedRandom when you experiment so that the problem appears consistently. For example, using RandomSeed[1], you'll see that randomPolygons4[[39]] // Area crashes the kernel. The polygon looks like this: Polygon[{{1/3, 1/3}, {0, 0}, {1/3, 1/3}, {0, 2/3}}] This seems like a problem for WRI. Aug 4 '19 at 20:13
• C.E. Thanks for replicating. Yes I used Map to sample a cross-section. Will report to support. Any known workaround for randomly generated polygons? Aug 4 '19 at 20:15
• It would be better if you list polygons that crash the kernel and then ask "how can I find a workaround for these polygons?" It would make the question clearer and easier to answer. Aug 4 '19 at 20:49
• @C.E, I think you identified the issue, it seems confined to polygons with repeated points. Using safePointSets = Table[randomThirdPoints[4], 100] // Select[DuplicateFreeQ] and then safePointSets // Map[Polygon] // Map[Area] seems to work. Aug 4 '19 at 20:52
• ok, I undeleted my answer. Aug 4 '19 at 20:58

The "possible issues" section of the documentation for Polygon says that

Degenerate polygons are not valid geometric regions

A degenerate polygon is a polygon that has two or more vertices which are the same.

The polygons that crash the kernel seem to be of this type. You can avoid the kernel crashes by not evaluating Area on such polygons, for example by using

If[DuplicatesFreeQ[vertices], Area[Polygon[vertices]], Undefined]


or you can modify your definition of randomThirdPoints so that it doesn't generate duplicate vertices:

randomThirdPoints[n_] := 1/3 RandomSample[Tuples[{0, 1, 2, 3}, 2], n]

• I tried randomPolygons4 // Map[DuplicateFreeQ] // Apply[And] and always get True (note I edited my question to use SetDelayed in the definition of randomPolygons4) Aug 4 '19 at 20:46
• @alancalvitti Using DuplicateFreeQ on polygons won't work, you have to run it on the list of vertices. Aug 4 '19 at 20:57
• I'm still experiencing crashes, using the distinct vertex generator, when sampling say 8-gons, and calling RegionQ (possibly some are polygons with holes). Aug 5 '19 at 17:15

One workaround for polygons with 4 different points:

Map[Area[DiscretizeGraphics[#]] &, randomPolygons4]

• does this work for any n distinct points? (Because then I could filter these before calling Polygon) Aug 4 '19 at 20:18
• @alancalvitti Probably yes, because DiscretizeGraphics creates a MeshRegion. Aug 4 '19 at 20:24
• Given currently the polygon vertices are sampled from a regular grid in unit square, how much distortion in the Area - DiscretizedGraphics[Area] can I expect with, say, uniformly random points in unit square? Aug 4 '19 at 20:31
• @alancalvitti If the meshregion consists of triangles there is no distortion I think. Aug 4 '19 at 20:37

Based on C.E.'s work, vertex deduplication must be done at the point of generation, eg:

randomThirdPoints[n_] := Module[{pts},
pts := Table[a {RandomChoice[{0,1,2,3}],RandomChoice[{0,1,2,3}]},{n}];
p=pts;
While[Not[DuplicateFreeQ[p]],p= pts]
p
]


this generated safe Polygons:

Table[randomThirdPoints[4], 100] // Map[DuplicateFreeQ] // Apply[And]


True

Area works for all of these, though Undefined still need to be filtered:

Table[Polygon@randomThirdPoints[4], 20] // Map[Area]


{4/27, 16/63, 1/6, 7/45, 1/3, 1/6, 1/2, Undefined, 1/9, 1/6, 2/5, \ 4/27, 1/9, 1/18, 11/54, 1/6, 1/9, 2/9, 1/2, 1/6}

Is there a more compact syntax? I don't think I've used a While loop in 20 years.

• You can use e.g. randomThirdPoints[n_] := 1/3 RandomSample[Tuples[{0, 1, 2, 3}, 2], n]` I think, but haven't thought about it too long. Aug 4 '19 at 21:31
• @C.E., thanks RandomSample works. Aug 5 '19 at 13:40