0

スクリプトは Processing 3.0.1 エディターで実行されます。私は達成したい:

  • 連続ループ
  • for ループの値を 10 に維持します (for ループ内の三角形の新しいセットごとに最も古いセットが置き換えられ、アニメーションが炎の流星のように見えます)

単純な if ステートメントでこれを達成できますか、それとも配列を作成する必要がありますか? 提案を受け付けます。

void setup() {
  size(504, 282);
  background(0);
}

void draw() {
  color from = color(255, 0, 0, 50);
  color to   = color(255, 255, 0, 300);
  color a = lerpColor(from, to, .33);
  color b = lerpColor(from, to, .66);
  for (int i=0; i<20; i++) {
    // 1st Stage
    float a1=random(0, 84);
    float b1=random(0, 47);
    float c1=random(84, 168);
    float d1=random(47, 94);
    float e1=random(168, 252);
    float f1=random(94, 141);
    fill(from);
    triangle(a1, b1, c1, d1, e1, f1);
    // 2nd Stage
    float a2=random(84, 168);
    float b2=random(47, 94);
    float c2=random(168, 252);
    float d2=random(94, 141);
    float e2=random(252, 336);
    float f2=random(141, 188);
    fill(a);
    triangle(a2, b2, c2, d2, e2, f2);
    // 3rd Stage
    float a3=random(168, 252);
    float b3=random(94, 141);
    float c3=random(252, 336);
    float d3=random(141, 188);
    float e3=random(336, 420);
    float f3=random(188, 235);
    fill(b);
    triangle(a3, b3, c3, d3, e3, f3);
    // 4th Stage
    float a4=random(252, 336);
    float b4=random(141, 188);
    float c4=random(336, 420);
    float d4=random(188, 235);
    float e4=random(420, 504);
    float f4=random(235, 282);
    fill(to);
    triangle(a4, b4, c4, d4, e4, f4);
    noStroke();
  }
  //saveFrame("####.png");
}
4

2 に答える 2

0

ステップ 1 : 1つの「ステージ」を描画するために知っておく必要があるすべてをカプセル化するクラスを作成します。あなたにとって、これはあなたの三角形の点かもしれません。

ステップ 2 :ArrayListそのクラスのインスタンスを保持する を作成します。

ステップ 3 :draw()関数内で、そのクラスのインスタンスを作成し、それを に追加しますArrayListframeCountこの変数をオペレーターで使用してmod、アニメーションの「速度」を制御できます。

ステップ 4 : にArrayList10 個を超えるオブジェクトが含まれている場合は、最初のオブジェクトを削除して最も古いオブジェクトを削除します。

ステップ 5 : オブジェクトをループして描画します。

すべてをまとめると、次のようになります (長方形の代わりに形状を使用すると:

ArrayList<MyShape> shapes = new ArrayList<MyShape>();

void setup() {
  size(500, 500);
}

void draw() {
  background(0);
  for (MyShape shape : shapes) {
    shape.draw();
  }

  if (frameCount % 10 == 0) {
    shapes.add(new MyShape());

    if (shapes.size() >= 10) {
      shapes.remove(0);
    }
  }
}

class MyShape {

  float x;
  float y;
  float r;

  public MyShape() {
    x = mouseX;
    y = mouseY;
    r = random(10, 20);
  }

  public void draw() {
    rect(x, y, r, r);
  }
}
于 2015-11-26T14:21:51.643 に答える
0

background() 関数は、draw() 関数に配置されると、新しいフレームごとにバッファーをクリアします。

void setup() {
  size(504, 282);
}

void draw() {
  background(0);
  color from = color(255, 0, 0, 50);
  color to   = color(255, 255, 0, 300);
  color a = lerpColor(from, to, .33);
  color b = lerpColor(from, to, .66);
  for (int i=0; i<20; i++) {
    float a1=random(0, 84);
    float b1=random(0, 47);
    float c1=random(84, 168);
    float d1=random(47, 94);
    float e1=random(168, 252);
    float f1=random(94, 141);
    fill(from);
    triangle(a1, b1, c1, d1, e1, f1);
    float a2=random(84, 168);
    float b2=random(47, 94);
    float c2=random(168, 252);
    float d2=random(94, 141);
    float e2=random(252, 336);
    float f2=random(141, 188);
    fill(a);
    triangle(a2, b2, c2, d2, e2, f2);
    float a3=random(168, 252);
    float b3=random(94, 141);
    float c3=random(252, 336);
    float d3=random(141, 188);
    float e3=random(336, 420);
    float f3=random(188, 235);
    fill(b);
    triangle(a3, b3, c3, d3, e3, f3);
    float a4=random(252, 336);
    float b4=random(141, 188);
    float c4=random(336, 420);
    float d4=random(188, 235);
    float e4=random(420, 504);
    float f4=random(235, 282);
    fill(to);
    triangle(a4, b4, c4, d4, e4, f4);
    noStroke();
  }
}
于 2015-12-11T15:06:41.210 に答える