【问题标题】:How do I change values,which are already used in a while loop?如何更改已在 while 循环中使用的值?
【发布时间】:2019-11-10 17:58:21
【问题描述】:

我正在尝试编写一个程序,以在矩形中显示具有固定间距“d=19”的球体。我只让它在 x 和 y 轴方向或对角轴上工作,而不是在矩形的整个区域上工作。我知道解决方案应该相当简单,但我似乎无法理解。

void setup() {
   size(600, 500);
   background(255);
}

void draw() {

    int x = 50;
    int y = 100;
    int r = 5;
    int d = 19;

    background(255);
    rect(x, y, mouseX-x, mouseY-y);

    while (y <= mouseY) {                 
        ellipse(x, y, 2*r, 2*r);

        while (x <= mouseX) {
          ellipse(x, y, 2*r, 2*r);
          x = x+d;
        }
        y = y+d;
    }
}

【问题讨论】:

    标签: processing


    【解决方案1】:

    您必须在嵌套循环的内部循环之前重置 x 坐标。圆圈必须从第一列的每一行开始:

    void draw() {
        int x=50;
        int y=100;
        int r =5;
        int d =19;
    
        background(255);
        rect(x, y, mouseX-x, mouseY-y);
    
        while (y <= mouseY) {      
    
            x = 50; // <--- start at the begin in each row
    
            while (x <= mouseX) {
                ellipse(x, y, 2*r, 2*r);
                x=x+d;
            }
            y=y+d;
        }
    }
    

    注意,要在二维网格中绘制对象,2 个嵌套循环就足够了。

    【讨论】:

      猜你喜欢
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 2020-10-03
      • 2016-10-04
      • 1970-01-01
      • 1970-01-01
      • 2015-10-31
      相关资源
      最近更新 更多