2
$\begingroup$

Updated

System Info: HONOR laptop AMD Ryzen 5 4600H 3Hz 6 cores 16GB Memory,
Windows 10 Pro 64 bit with updates.

Mathematica Info: Versions 12.2 and 13.2, both on the same laptop.

Examples:

p1 = RandomPolygon[{"Simple", 50}, DataRange -> {{1, 2}, {1, 2}}];
p2 = RandomPolygon[{"Simple", 50}, DataRange -> {{1, 2}, {1, 2}}];  

Timing[RegionIntersection[p1, p2] === EmptyRegion[2]]  

WM 12.2: 0.1-0.3 s vs WM 13.2: 3.1-3.6 s

Timing[RegionDifference[p1, p2]] 

WM 12.2: 0.3-0.34 s vs WM 13.2: 3.4-3.5 s

Timing[RegionUnion[p1, p2]]

WM 12.2: 0.1-0.2 s vs WM 13.2: 0.7-0.9 s

Other operations (graphs, big numbers) do not show such differences, but I still checking.

More specific question

For one demo, I'd like to create shapes like this one:
enter image description here
Not too complex (without holes and self-intersections), so the output is just simple polygon with 30-50 vertices:

bf[p_, s_] := 
  BSplineFunction[p, SplineClosed -> True, SplineDegree -> s];
initialShape = 
  With[{pts = RandomReal[{0.2, 0.6}, {9, 2}]*CirclePoints[9]},
   Polygon[Table[ bf[pts, 4][t], {t, 0, 1, 0.02}]]
   ];
shape[center_, size_] := 
  TranslationTransform[center][
   ScalingTransform[{size, size}][initialShape]];

For further I need to analyze the intersections of these figures. The problem with RegionDisjoint has already been described here, and in version 13.2 it still remained:

RegionDisjoint[shape[{1., 1.}, 1.], shape[{5., 5.}, 2.]]

Output:
enter image description here

OK, we can use RegionIntersection:

isNonIntersect[shape1_, shape2_] :=
  RegionIntersection[shape1, shape2] === EmptyRegion[2];

It works, but I found big oddities with the timing of RegionIntersection:

Result:
True Version 12.2: 0.06-0.08s Version 13.2: 1.2-1.3s
False Version 12.2: 0.0s Version 13.2: 0.01-0.2s

Why does it take longer to get True, and why Mathematica 13.2 is so slow?!
(all it runs on the same laptop with Win 10)

$\endgroup$
9
  • 2
    $\begingroup$ I have no answer to this but I'd suggest applying BoundaryDiscretizeRegion or DiscretizeRegion to the polygon; as far as I know it should result equivalent region, at least for polygons with machine-precision coordinate values. Also, mesh regions tend to behave better on intersections etc. than polygons. $\endgroup$
    – kirma
    Commented Jan 20, 2023 at 7:26
  • $\begingroup$ Can you provide an example of a specific pair of non-overlapping regions which show this performance difference? That is, a SeedRandom value and shape specifications. $\endgroup$
    – kirma
    Commented Jan 20, 2023 at 7:59
  • $\begingroup$ @kirma Yes, soon, I'm preparing a separate question about the performance of version 13.2, it is slower than 12.2 in almost everything $\endgroup$
    – lesobrod
    Commented Jan 20, 2023 at 8:23
  • 1
    $\begingroup$ Please see How to report a bug in Mathematica. Making a post here is a great way to get a workaround for something, or confirmation that others can reproduce. But if bugs are to be fixed they need to be reported to Wolfram directly. $\endgroup$
    – Jason B.
    Commented Jan 20, 2023 at 16:58
  • 1
    $\begingroup$ @kirma You was almost right thanks! Just not DiscretizeRegion but BoundaryMeshRegion works very well $\endgroup$
    – lesobrod
    Commented Jan 21, 2023 at 7:28

1 Answer 1

2
$\begingroup$

If simple polygons are given by lists of their vertices, then the fastest and most reliable way to check their intersection is:

myRegion[pts_] := 
  With[{bnd = Append[Range@Length@pts, 1]}, 
   BoundaryMeshRegion[pts, Line@bnd]];

isIntersect[verts1_, verts2_] :=
  UnsameQ[RegionIntersection[myRegion/@{verts1, verts2}], 
   EmptyRegion[2]];

It's 10-15 times faster than UnsameQ[RegionIntersection[Polygon@verts1, Polygon@verts2], EmptyRegion[2]] both on 12.2 and 13.2.

$\endgroup$

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.