021. How Many Voxels Does the Ray Pass Through?
Let a cube with side length 1 be called a voxel. Given natural numbers , , , stack voxels to form a rectangular parallelepiped with side lengths , , and respectively. Determine the number of voxels through which one diagonal of this rectangular parallelepiped passes internally.
The positions of voxels can be represented using coordinates as follows.
This is represented by the highest number among the coordinates of the eight vertices of a single voxel. Now, when the diagonal is defined as the ray moving from the origin to the point , the total coordinate change is .
- The ray passes through a voxel’s face (excluding the boundary lines)
- When moving to the next voxel, only one of the coordinates increases by 1.
- The ray passes through a voxel’s edge (excluding vertices)
- When moving to the next voxel, only two of the coordinates increase by 1.
- The ray passes through a voxel’s vertex
- When moving to the next voxel, all of the coordinates increase by 1.
That is, can be viewed as a value calculated assuming the ray only passed through the faces of voxels. Therefore, this value must be adjusted to obtain the actual number of voxels traversed. When is defined as the function representing the greatest common divisor, the following can be determined.
- The ray passes through edges parallel to the -axis.
- The ray passes through edges parallel to the -axis.
- The ray passes through edges parallel to the -axis.
assumes the ray traverses only faces, but when it actually crosses edges, two coordinate changes occur in a single movement. Therefore, the number of edges traversed by the ray must be subtracted. Additionally, cases where the ray passes through a vertex, causing all three coordinates to change, must be included. However, since this subtraction causes such cases to be deducted redundantly, must be added back. Therefore, the number of voxels traversed by this ray is as follows.