MATLAB: Determine if a point is inside a cube

cube

Hi! There are given 8 vertices of a cube and a random point. How can I define that the point lies inside the cube? I wanted to create 2 vectors from the random point. These vectors should be parallel to X and Y axis (Z is not needed here). Then, if vectors intersect with cube sides 4 times, then the point lies inside a cube. But this seems to be too complex approach for this problem. Does someone know a simpler way? A code snipped would be very helpful. Thanks.

Best Answer

The way I'd do it is to call convhulln(). Basically it gives you the vertices of your set of 3D vertices that are on the convex hull. The convex hull is what you'd get if you basically wrapped a balloon around your points.
So all you have to do is to pass in your cube coordinates concatenated with your "test" point and see if it returns the cube coordinates. If it doesn't then the point is outside. If it does, then the cube is the convex hull and that must mean your test point is inside the cube. It's one line of code to call convhulln() and another to do the check to see if what it returns matches your cube coordinates. You should be able to do it. If you can't then it's your turn to supply us with code that generates cube coordinates and test points (one inside and one outside) so that we can do those two lines for you.