I am trying to find a way to determine if a line segment drawn between two points in 3D space will intersect another 3D body (rectangular prism) that is defined by regions using (x, y, z) vertices.
Is there a way to do this in Matlab?

- 3
- 2
-
Is rectangular prism (box) axis-aligned? – MBo Sep 29 '22 at 05:03
-
Yes, it is aligned with the z-axis. – Michelle Sep 29 '22 at 20:14
1 Answers
Make equations for box faces. In the case of axis-aligned box they are simple like
for facet1 perpendicular to OX axis:
x - x1 = 0
y1 <= y <= y2
z1 >= z <= z2
At first check that segment ends don't lie at the same side of all facets, substituting their coordinates in plane equations. If signs of result for both segment ends are similar, segment does not intersect this plane. If all 6 planes are excluded, there in no intersection, but check if both ends lie inside the box (if this case is considered as intersection)
Now work with faces that provide distinct signs.
Make parametric representation of line segment
x(t) = p0.x + (p1.x - p0.x) * t
y(t) = p0.y + (p1.y - p0.y) * t
z(t) = p0.z + (p1.z - p0.z) * t
and substitute coordinates in plane equations like this:
(p0.x + (p1.x - p0.x) * t) - x1 = 0
Get t
parameter. If it's in range 0..1, continue: put t
value in
y = p0.y + (p1.y - p0.y) * t
and check that result is in range y1..y2, similar for z. If yes - segment does intersect facet1, otherwise continue with other facets.
Perhaps there are geometric libraries in Matlab providing some 3D line-clipping algorithm, in this case just use them.

- 77,366
- 5
- 53
- 86