# Find the Closest and Farthest Points of a Cube

This has to do with collision detection between a ray and a cube. I have a camera position that I am looking from and a ray is being shot into the scene which contains a cube.

I have the ray starting position and direction in 3D space. The cube is defined by its center position coordinates along with height, width, and depth.

I am following an algorithm I found online, and a task that I have become stuck on is finding the closest and farthest points of the cube. Google searches haven't proved promising for me thus far, I would appreciate any suggestions!

-
The farthest point on the cube has to be a vertex, so that part is easy. (In general, the farthest point in any closed convex set has to be on its boundary - just check that it's true on any line segment - and all the faces and edges of the cube are also convex.) – Greg Martin Feb 28 '12 at 0:18
Yes, I believe you are right. I wasn't quite sure before but this certainly makes intuitive sense. – Aaron Feb 28 '12 at 0:46
To clarify the question, do you mean the closest and farthest points on a fixed ray? Or do you mean the closest and farthest points on any ray from a fixed point? – Code-Guru Jul 19 '12 at 14:47

If you mean to say that you are looking for the entrance and exit points of the ray through the cube, I'd do this:

Given your ray $r(t)=\vec u + \vec vt$, find the $t$ values of the intersections between this ray and the planes that make up the faces of the cuboid. Do not, at this point, filter out negative $t$ values.

Sort the intersections by these $t$ values; if no two of the highest three $t$ values are from opposite faces, then the middle two $t$ values define the entrance and exit points of your ray. Now you can figure out if your ray actually hits these points, or if they're actually behind the ray.

There are some special cases here: if your ray never touches some of the planes, then you don't need to keep them around, but your ray only touches the box if the ray is between opposite pairs of planes. This also reduces the list of intersections that you need to check for being on opposite sides by 1, for every 2 planes you don't hit.

-

Do you mean the longest distance of a ray drawn inside of the cube? If yes, then you need to draw the ray from one vertex to the other side in that way it forms a triangle with two diagonals of the square faces and the height will be the side of square face.

-

Assuming your camera is at the origin $(x,y,z)=(0,0,0)$ and that your cube has side length $a$, then your cube can be centered at $$(x,y,z)=(\frac{a}{2},\frac{a}{2},\frac{a}{2})$$ and the point furthest from the camera is at $$(x,y,z)=(a,a,a)$$.

If you're looking for the distance, $s$, from the camera to the farthest point, you can use the Pythagorean theorem in three dimensions. This is the distance formula starting measuring the distance from the origin (your camera) to the farthest point (the opposing vertex). Geometrically, this is the diagonal of the cube:

$$s = \sqrt{x^2 + y^2 + z^2}$$ $$s = \sqrt{a^2 + a^2 + a^2}$$ $$s = a\sqrt{3}$$

-