# Intersection between shape and line

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

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


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.

Update:

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}


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

CanonicalizePolyhedron[cub][[1]]


which gives the vertices of the cuboid....

• 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. Commented Jan 2, 2020 at 19:29
• It seems like a bug that RegionIntersection always returns an EmptyRegion while trying to compute an intersection in your first example though. Commented Jan 2, 2020 at 19:32
• Your first example doesn’t work because a list is not a region. Try changing it to a RegionUnion of Line objects. Commented Jan 2, 2020 at 19:42

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