Determine if a point is inside a cube

26 visualizaciones (últimos 30 días)
Lulu
Lulu el 16 de Nov. de 2011
Respondida: sajjad nasiri el 18 de Jul. de 2018
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.
  1 comentario
Honglei Chen
Honglei Chen el 16 de Nov. de 2011
Could you give a little more information regarding the issue? Is the cube really a right-angle equal-side-length cube? Is the orientation arbitrary or it has to be aligned with axes? Maybe an example can clarify these.

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 16 de Nov. de 2011
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.
  2 comentarios
Lulu
Lulu el 16 de Nov. de 2011
Thanks. I wrote the following code according to your idea:
% Create a cube
xyz = [0 0 0; 1 1 1; 0 0 1; 0 1 0; 1 0 0; 1 0 1; 1 1 0; 0 1 1];
random_point = [1 2 1]; % lies outside the cube
xyz = [xyz; random_point];
K = convhulln(xyz);
len = length(xyz(:,1));
inside = any(any(K==len))
Please let me know if there is a better implementation of your idea.
Image Analyst
Image Analyst el 17 de Nov. de 2011
Well K looks more complicated than the 2D case. I'd have to think about it. But you'd have to call convhulln twice (once for cube and once for cube + extra points) and use ismember to compare that each row of K1 is included in K2.

Iniciar sesión para comentar.

Más respuestas (1)

sajjad nasiri
sajjad nasiri el 18 de Jul. de 2018
Hi. Check this: https://uk.mathworks.com/matlabcentral/fileexchange/68228-incube

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by