【问题标题】:For loop for Pyramid of Circles in Processing/Java处理/Java中圆圈金字塔的for循环
【发布时间】:2017-10-15 22:41:39
【问题描述】:

我试图在处理过程中绘制一个由 10 个圆圈组成的金字塔,但我想不出一种将这段代码压缩成一个干净循环的好方法。 lights[c].display() 函数只是在当前翻译的中心绘制一个椭圆和一些文本。我想保留 gap 变量作为我可以用来更新圆圈之间间距的东西。我只是还没弄清楚如何以导致金字塔的方式增加行和列。总会有10个圆圈。谢谢你的帮助!



int gap = 30;

public void display() {
  int c = 0;
  pushMatrix();
    translate(0, 0);
    lights[c++].display();
  popMatrix();

  pushMatrix();
    translate(-gap, 2*gap);
    lights[c++].display();
  popMatrix();

  pushMatrix();
    translate(gap, 2*gap);
    lights[c++].display();
  popMatrix();

  pushMatrix();
    translate(-2*gap, 4*gap);
    lights[c++].display();
  popMatrix();

  pushMatrix();
    translate(0, 4*gap);
    lights[c++].display();
  popMatrix();

  pushMatrix();
    translate(2*gap, 4*gap);
    lights[c++].display();
  popMatrix();

  pushMatrix();
    translate(-3*gap, 6*gap);
    lights[c++].display();
  popMatrix();

  pushMatrix();
    translate(-gap, 6*gap);
    lights[c++].display();
  popMatrix();

  pushMatrix();
    translate(gap, 6*gap);
    lights[c++].display();
  popMatrix();

  pushMatrix();
    translate(3*gap, 6*gap);
    lights[c++].display();
  popMatrix();
}

【问题讨论】:

    标签: java for-loop while-loop drawing processing


    【解决方案1】:

    您可以从这个简单的解决方案开始

    int[] tx = new int[]{0, -1, 1, ...};
    int[] ty = new int[]{0, 2, 2, ...};
    
    for(int c = 0; c < circlesCount; c++) {
      pushMatrix();
      translate(tx[c] * gap, ty[c] * gap);
      lights[c].display();
      popMatrix();
    }
    

    【讨论】:

    • 这当然有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    相关资源
    最近更新 更多