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]]

enter image description here

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}]
  • 1
    $\begingroup$ Removed the {} still too slow for another examples, we can test my example in my Review. $\endgroup$
    – cvgmt
    Mar 7, 2022 at 23:57
  • $\begingroup$ 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. $\endgroup$
    – Syed
    Mar 8, 2022 at 7:36

2 Answers 2


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]}

enter image description here


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

Clear[c1, c2];
c1 = Cylinder[RandomReal[1, {2, 3}], 1]
c2 = Cylinder[RandomReal[1, {2, 3}] + RandomReal[{-2, 2}], 1]
{Graphics3D[{c1, c2}], RegionDisjoint[c1, c2]}
  • $\begingroup$ I tried a few times. By removing the {}, sometimes it is fast but sometimes it is slow...No idea why. $\endgroup$
    – Burrawang
    Mar 8, 2022 at 2:37
  • $\begingroup$ @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. $\endgroup$
    – Syed
    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}.

enter image description here

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


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.