# How to quickly test if two regions intercept?

If I randomly generated two cylinders, how do I test if they intercept? For example:

c1 = Cylinder[{RandomReal[1, {2, 3}]}, 1];
c2 = Cylinder[{RandomReal[1, {2, 3}] + RandomReal[{-2, 2}]}, 1];
Show[Graphics3D[{c1, c2}, Axes -> True]]


If I use  BooleanRegion[And, {c1, c2}]

The program runs very slow if two objects have no interception.

----------------------update---------- The program still run slowly when { } were removed from the Cylinder function, but sometimes it runs fast.

c2 = Cylinder[RandomReal[1, {2, 3}] + RandomReal[{-2, 2}], 1];
Show[Graphics3D[{c1, c2}, Axes -> True]]
BooleanRegion[And, {c1, c2}]

• Removed the {} still too slow for another examples, we can test my example in my Review. Commented Mar 7, 2022 at 23:57
• Please unaccept the answer as it has not resolved the problem. I mentioned in my answer that it could be a bug and it seems like one.
– Syed
Commented Mar 8, 2022 at 7:36

Calculate the distant of the two 3d objects.

{c1, c2} =
Table[Cylinder[{RandomReal[1, {2, 3}] + RandomReal[{-2, 2}]}, 1], 2];
epsilon = .0001;
intersectionQ[c1_, c2_] :=
NMinimize[{EuclideanDistance[{x, y, z}, {u, v, w}], {x, y,
z} ∈ c1, {u, v, w} ∈ c2}, {x, y, z, u, v,
w}][[1]] < epsilon;
{Graphics3D[{c1, c2}, Axes -> True], intersectionQ[c1, c2]}


Review

For another example, remove the brace {} still too slow.

Clear[c1, c2];
SeedRandom[3];
c1 = Cylinder[RandomReal[1, {2, 3}], 1]
c2 = Cylinder[RandomReal[1, {2, 3}] + RandomReal[{-2, 2}], 1]
{Graphics3D[{c1, c2}], RegionDisjoint[c1, c2]}

• I tried a few times. By removing the {}, sometimes it is fast but sometimes it is slow...No idea why. Commented Mar 8, 2022 at 2:37
• @cvgmt, your solution performs consistently better than the built-in RegionDisjoint. Please file this as a bug and retag the question. For a routine calculation, a time such as 90s on a 3+GHz machine is nothing short of embarassing.
– Syed
Commented Mar 8, 2022 at 7:33

The problem stated in OP is that for the no intersection case, it takes too long. So after some experimentation ...

I used @cvmgt 's method to generate cylinders {c1,c2}. Notice the three braces at the beginning and end of the endpoints. According to the docs, cylinders end points are defined as a list of lists. So I manually removed the additional pair calling those cylinders {c3,c4}.

The community may want to determine if this is a bug or if this is intended.