如何在处理中重复模式

How to repeat pattern in Processing

我有以下代码。我正在尝试使用 TRANSLATE 在每次循环传递时向下移动 Y 位置,但它不遵守变量中指示的 Y 位置。建议?

int vShapePosX = 0;
int vShapePosY = 0;

int[] myVertX = { ... };
int[] myVertY = { ... };

void draw() {
    int j = 0;
    while (j < 10) {
        fDrawRow();
        translate(vShapePosX, vShapePosY);
        vShapePosY = vShapePosY + 100;
        println(j);
        j = j + 1;
    }
    stop();
}
        
void fDrawRow(){
    int i = 0;
    while (i < 24) {
        // -------------- START -- DRAW SHAPE
        beginShape();
        int vCount = 0;
        while (vCount < 24) {
            vertex(myVertX[vCount], myVertY[vCount]);
            myVertX[vCount] = myVertX[vCount] + 85;     
            vCount++;                                   
        }
        endShape();
        // -------------- END -- DRAW SHAPE
        i = i + 1;
    } // end WHILE loop
} // end function

translate() does not set a translation matrix. It constructs a new translation matrix and and multiplies the current matrix by the new matrix. Thus you have to save the current matrix by pushMatrix() before you apply the translation and you have to restore the matrix by popMatrix()绘制对象后
此外,我建议使用 for 循环并在循环之前重置变量 vShapePosY

void draw() {
    
    vShapePosY = 0;
    for (int j = 0; j < 10; j++) {

        pushMatrix(); // <--- save current matrix

        translate(vShapePosX, vShapePosY); // <--- change current matrix 
        fDrawRow();

        popMatrix(); // <--- restore current matrix 
        
        vShapePosY = vShapePosY + 100;
    }
    stop();
}

另一种方法是在循环中通过 (0, 100) 逐渐改变翻译:

void draw() {

    translate(vShapePosX, vShapePosY);

    pushMatrix();
    for (int j = 0; j < 10; j++) { 
        fDrawRow();
        translate(0, 100); // <--- change current matrix 
    }
    popMatrix();

    stop();
}