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.

  • $\begingroup$ 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. $\endgroup$
    – C. E.
    Aug 4 '19 at 20:13
  • $\begingroup$ 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? $\endgroup$ Aug 4 '19 at 20:15
  • $\begingroup$ 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. $\endgroup$
    – C. E.
    Aug 4 '19 at 20:49
  • $\begingroup$ @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. $\endgroup$ Aug 4 '19 at 20:52
  • $\begingroup$ ok, I undeleted my answer. $\endgroup$
    – C. E.
    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]
  • $\begingroup$ I tried randomPolygons4 // Map[DuplicateFreeQ] // Apply[And] and always get True (note I edited my question to use SetDelayed in the definition of randomPolygons4) $\endgroup$ Aug 4 '19 at 20:46
  • $\begingroup$ @alancalvitti Using DuplicateFreeQ on polygons won't work, you have to run it on the list of vertices. $\endgroup$
    – C. E.
    Aug 4 '19 at 20:57
  • $\begingroup$ I'm still experiencing crashes, using the distinct vertex generator, when sampling say 8-gons, and calling RegionQ (possibly some are polygons with holes). $\endgroup$ Aug 5 '19 at 17:15

One workaround for polygons with 4 different points:

Map[Area[DiscretizeGraphics[#]] &, randomPolygons4] 
  • $\begingroup$ does this work for any n distinct points? (Because then I could filter these before calling Polygon)` $\endgroup$ Aug 4 '19 at 20:18
  • $\begingroup$ @alancalvitti Probably yes, because DiscretizeGraphics creates a MeshRegion. $\endgroup$ Aug 4 '19 at 20:24
  • $\begingroup$ 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? $\endgroup$ Aug 4 '19 at 20:31
  • $\begingroup$ @alancalvitti If the meshregion consists of triangles there is no distortion I think. $\endgroup$ 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}];
    While[Not[DuplicateFreeQ[p]],p= pts]

this generated safe Polygons:

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


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.

  • 2
    $\begingroup$ 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. $\endgroup$
    – C. E.
    Aug 4 '19 at 21:31
  • $\begingroup$ @C.E., thanks RandomSample works. $\endgroup$ Aug 5 '19 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.