Systellence - 26 days ago 7

C# Question

I need the best possible solution, and if possible not dirty code, but rather some smart code.

I am not familiar with 3D programming.

I have to write a function that will return if a point is in 3D space (x, y, z). The range of the cube in space will be supplied (e.g. range { {1,2,1}, {5,6,4} })

Any ideas?

Answer Source

It is enough to test whether the given point has all coordinates lying to between the points. So if (x1, y1, z1) is the minimum corner and (x2, y2, z2) is the max point, to test the point (x, y, z), test verify that x1 <= x <= x2, and similarly for y and z.

If this doesn't seem obvious, just realize that a cube aligned with the coordinate axes is the region lying left of the plane x = x1, and right of the plane x = x2, above the plane y = y1 and below y = y2, etc. The points in the cube are those that satisfy all six inequalities simultaneously. This is precisely what you are verifying.

If the cube is not aligned with the coordinate axes you cannot determine it from two corners. Rather, it is described by giving inequalities for the planes that determine the size faces. You would need to check that all six are satisfied. This can always be written as a matrix inequality. In this case, a 6x3 matrix, three columns since the points are in 3d space and one row for each constraint. This is the general case, but is overkill for the problem as stated.