Polygons crash kernel?
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]
One workaround for polygons with 4 different points:
Map[Area[DiscretizeGraphics[#]] &, randomPolygons4]
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 Polygon
s:
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.