OpenSCAD 如何访问矩阵中的值

OpenSCAD how to access a value within a Matrix

如何在 OpenSCAD 中索引矩阵或在循环中迭代它? 我正在尝试通过 forloop 访问分配给坐标的值并将其分配给它们的单个变量,如下所示,或者至少能够在 Matrix 中单独访问这些值。

for ( coordinates = [ [  15,  15,  2],
                      [  15, -15,  2],
                      [ -15, -15,  2],
                      [ -15,  15,  2] ]) 
{
    x = coordinates[0];
    y = coordinates[1];
    z = coordinates[2];
    translate([x+4, y, z]){ 
        cube([x,y,z]);
    }
}

首先,标准变量是在 OpenSCAD 的编译时设置的,而不是 运行 时间(official documentation 说明),因此您不能在循环中为它们赋值。您必须内联对 coordinates 的引用才能使用其中的值。

第二个问题是你不能制作一个负尺寸的立方体,或者我猜测我从提供的循环的第二次到第四次迭代中没有得到任何输出。您可以在 abs() 调用中包装传递到多维数据集的值以获取绝对值以确保它是正数。

这是一个内联 coordinates 变量并使用 abs() 将正值传递给 cube() 的工作示例:

for ( coordinates = [ [  15,  15,  2],
                      [  15, -15,  2],
                      [ -15, -15,  2],
                      [ -15,  15,  2] ])
{
    translate([coordinates[0] + 4, coordinates[1], coordinates[2]]) { 
        cube([abs(coordinates[0]), abs(coordinates[1]), abs(coordinates[2])]);
    }
}