6
$\begingroup$

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.

$\endgroup$
8
  • $\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, 2019 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, 2019 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, 2019 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, 2019 at 20:52
  • $\begingroup$ ok, I undeleted my answer. $\endgroup$
    – C. E.
    Aug 4, 2019 at 20:58

3 Answers 3

7
$\begingroup$

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]
$\endgroup$
3
  • $\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, 2019 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, 2019 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, 2019 at 17:15
4
$\begingroup$

One workaround for polygons with 4 different points:

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

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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