I am having some trouble figuring out if 2 rectangular prisms touch or overlap. I only have the highX,Y,Z and lowX,Y,Z of both rectangular prisms. Here is what I have so far:
public boolean overlaps(AreaSelection other) {
boolean Xs = (lowX <= other.getHighestX()) && (other.getLowestX() <= highX);
boolean Ys = (lowY <= other.getHighestY()) && (other.getLowestY() <= highY);
boolean Zs = (lowZ <= other.getHighestZ()) && (other.getLowestZ() <= highZ);
return (Xs && Ys && Zs);
}
Does anyone know if this is correct or not? And if not, what is the solution? Thanks!
Two rectangular prisms with edges parallel to the coordinate axes overlap if and only if the respective three pairs of projected intervals on coordinate axes all overlap.
So it makes sense to have a “utility” method which checks for overlap of intervals, which we call three times to check for overlap of the prisms. We assume this method will be called with the intervals’ endpoints properly ordered:
The original code would then become:
Note that in this approach overlap may consist of a single point of intersection (whether in one dimension or three).