I have a shape with some lines. Both are graphics in Mathematica.

shape =  Cuboid[{0, 0, 0.5}, {0.5, 0.5, 0}];
    Line[RandomReal[1, {2, 3}]]}, {100}], {Opacity[0.2], shape}}]

enter image description here

I just want to determine, for each face of the cuboid, if a line has intersected it. I just need a true or false. I don't need the intersection coordinate, just whether the line has intersected or not. Note that some lines could intersect multiple faces.

I tried using the region tools in Mathematica, but since Line isn't a region, it doesn't work.

RegionIntersection[#, shape] & /@ 
 Table[Line[RandomReal[1, {2, 3}]], {100}]

I'm trying to determine if there is a pre-existing Mathematica function which can be used to quickly to find if an intersection exists for each component surface of the Cuboid.


From reading this question, This seems to work better to get the intersections with the cuboid

cub = Cuboid[{-2, -1, 0}, {2, 2, 2}];
lines2 = Table[{Hue[RandomReal[]], 
    Line[RandomReal[5, {2, 3}]]}, {100}];

(intersections = {{[email protected], 
       cub}, {#, 
         RegionIntersection[#2, cub] /. {_EmptyRegion -> Nothing, 
           Line -> Point}} & @@@ lines2};) // 
  AbsoluteTiming // #[[1]]/(n 5) &

Graphics3D[{Thick, lines2, AbsolutePointSize@12, intersections}, 
 ImageSize -> 800]

intersections = {{#, 
     RegionIntersection[#2, cub] /. {_EmptyRegion -> Nothing, 
       Line -> Point}} & @@@ lines2}

enter image description here

However, I'm still struggling in determining which surface the line went through...I feel there might be a solution using


which gives the vertices of the cuboid....

  • $\begingroup$ Try RegionIntersection with RegionBoundary[shape]. You'll get either EmptyRegion or a Point with 0.5 in one or more coordinate place, this will indicate through what face the line went through. $\endgroup$
    – swish
    Jan 2, 2020 at 19:29
  • $\begingroup$ It seems like a bug that RegionIntersection always returns an EmptyRegion while trying to compute an intersection in your first example though. $\endgroup$
    – swish
    Jan 2, 2020 at 19:32
  • $\begingroup$ Your first example doesn’t work because a list is not a region. Try changing it to a RegionUnion of Line objects. $\endgroup$
    – Carl Woll
    Jan 2, 2020 at 19:42

1 Answer 1

lines = Line /@ RandomReal[1, {100, 2, 3}];

intersections = Function[x, 
   DeleteCases[RegionIntersection[x, #] & /@ lines, _EmptyRegion, All]];

intersectsLinesQ = intersections[#] != {} &;

faces = Polygon /@ RegionBoundary[shape][[1]]
intersectsLinesQ /@ faces

{True, False, True, True, False, False}

facesThatIntersectLines = Select[intersectsLinesQ] @ faces;

Graphics3D[{Opacity[.1], faces, 
 {Opacity[.5], RandomColor[], #, Opacity[1], PointSize[Large],
      intersections @ #} & /@ facesThatIntersectLines, 
  Opacity[1], {RandomColor[], #} & /@ lines}]

enter image description here


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.