8 个立方体的最小/最大角,围绕原点形成一个立方体
min / max corners for 8 cubes forming a single cube around an origin
这是一个更通用的数学/代码问题,在我的例子中,我需要它用于 Godot/GDScript。我有一个虚拟立方体定义为具有厚度偏移的原点,我循环遍历每个轴上的最小值和最大值以执行每个单位的操作。普通立方体因此表示为:
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)
结果是我想将这个立方体细分为 8 个相互完美接触的立方体。我需要有一个 mins_0
/ maxs_0
, mins_1
/ maxs_1
, ... , mins_7
/ maxs_7
每个代表的一部分立方体。
我被困在数学上:我不知道如何转置原点和厚度偏移,显然要保持顺序以便任何循环都从正确的位置开始(mins.# < maxs.#
总是)。我只知道其中两个很明显的组合:
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)
如何按正确顺序填充其他 6 个空格以覆盖剩余区域?
如果使用矢量运算,这会很多简单,但我们可以使用原始坐标来完成:
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)
这是一个更通用的数学/代码问题,在我的例子中,我需要它用于 Godot/GDScript。我有一个虚拟立方体定义为具有厚度偏移的原点,我循环遍历每个轴上的最小值和最大值以执行每个单位的操作。普通立方体因此表示为:
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)
结果是我想将这个立方体细分为 8 个相互完美接触的立方体。我需要有一个 mins_0
/ maxs_0
, mins_1
/ maxs_1
, ... , mins_7
/ maxs_7
每个代表的一部分立方体。
我被困在数学上:我不知道如何转置原点和厚度偏移,显然要保持顺序以便任何循环都从正确的位置开始(mins.# < maxs.#
总是)。我只知道其中两个很明显的组合:
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)
如何按正确顺序填充其他 6 个空格以覆盖剩余区域?
如果使用矢量运算,这会很多简单,但我们可以使用原始坐标来完成:
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)