Search code examples
mathvectorgdscript

min / max corners for 8 cubes forming a single cube around an origin


This is a more generic math / code question, in my case I need it for Godot / GDScript. I have a virtual cube defined as an origin point with a thickness offset, I loop through its mins and maxs on each axis to preform actions per unit. The normal cube is thus represented as:

const org = Vector3(32, 48, 64)
const thick = 16
const mins = Vector3(org.x - thick, org.y - thick, org.z - thick)
const maxs = Vector3(org.x + thick, org.y + thick, org.z + thick)

for x in range(mins.x, maxs.x, thick):
    for y in range(mins.y, maxs.y, thick):
        for z in range(mins.z, maxs.z, thick):
            var vec = Vector3(x, y, z)

What happens is I want to subdivide this cube into 8 cubes perfectly touching each other. I need to have a mins_0 / maxs_0, mins_1 / maxs_1, ... , mins_7 / maxs_7 each representing a section of the cube.

I'm stuck on the math: I don't know how to transpose the origin and thickness offsets, obviously keeping the order so that any loop starts from the proper location (mins.# < maxs.# always). I only know two of the combinations which are obvious:

mins_0 = Vector3(org.x - thick, org.y - thick, org.z - thick)
maxs_0 = Vector3(org.x, org.y, org.z)
mins_4 = Vector3(org.x, org.y, org.z)
maxs_4 = Vector3(org.x + thick, org.y + thick, org.z + thick)

How do I fill the other 6 spaces in proper order to cover the remaining area?


Solution

  • This would be a lot simpler with vector arithmetic, but we can do it with the raw coordinates:

    mins_0 = Vector3(org.x - thick, org.y - thick, org.z - thick)
    maxs_0 = Vector3(org.x,         org.y,         org.z)
    
    mins_1 = Vector3(org.x - thick, org.y - thick, org.z)
    maxs_1 = Vector3(org.x,         org.y,         org.z + thick)
    
    mins_2 = Vector3(org.x - thick, org.y,         org.z - thick)
    maxs_2 = Vector3(org.x,         org.y + thick, org.z)
    
    mins_3 = Vector3(org.x - thick, org.y,         org.z)
    maxs_3 = Vector3(org.x,         org.y + thick, org.z + thick)
    
    mins_4 = Vector3(org.x,         org.y - thick, org.z - thick)
    maxs_4 = Vector3(org.x + thick, org.y,         org.z)
    
    mins_5 = Vector3(org.x,         org.y - thick, org.z)
    maxs_5 = Vector3(org.x + thick, org.y,         org.z + thick)
    
    mins_6 = Vector3(org.x,         org.y,         org.z - thick)
    maxs_6 = Vector3(org.x + thick, org.y + thick, org.z)
    
    mins_7 = Vector3(org.x,         org.y,         org.z)
    maxs_7 = Vector3(org.x + thick, org.y + thick, org.z + thick)