Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlapping cubes

Tags:

geometry

I'm trying to determine if two cubes overlap. I've read up on overlapping rectangles, but I'm not sure how to translate it into the third dimension.

My goal is to generate a number of randomly positioned and sized non-overlapping cubes.

These cubes are represented on a x,y,z Cartesian plane.

like image 240
Emma Avatar asked Apr 16 '26 13:04

Emma


1 Answers

The accepted answer is wrong and very confusing. Here is what I have come up with.

Determining overlap in the x plane

    if (cubeA.maxX > cubeB.minX)
    if (cubeA.minX < cubeB.maxX)

Determining overlap in the y plane

    if (cubeA.maxY > cubeB.minY)
    if (cubeA.minY < cubeB.maxY)

Determining overlap in the z plane

    if (cubeA.maxZ > cubeB.minZ)
    if (cubeA.minZ < cubeB.maxZ)

if you AND all of these conditions together and the result is true, you know that the cubes intersect at some point.

Credit: https://silentmatt.com/rectangle-intersection/

like image 122
sanch Avatar answered Apr 19 '26 04:04

sanch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!