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....
RegionIntersection
withRegionBoundary[shape]
. You'll get eitherEmptyRegion
or aPoint
with 0.5 in one or more coordinate place, this will indicate through what face the line went through. $\endgroup$RegionIntersection
always returns anEmptyRegion
while trying to compute an intersection in your first example though. $\endgroup$