OneHourProcessing 四日目

OneHourProcessing 四日目

三日坊主で終わりませんでした!!!
うっせやろお前…(自分を信用していない人の図)
本日作った物はこちら。

作るのにかかった時間は「59分14秒」。
時間以内、やったぜ。

コード

Square[][] Squares;
Particle[] particles;

int Time;

void setup()
{
  size(800, 450);
  squareReset();
  particleReset();
}

void draw()
{
  Time++;
  if (Time % 120 == 0)
    squareReset();

  background(0);

  noFill();
  stroke(255);
  for (int x = 0; x < Squares.length; x++)
    for (int y = 0; y < Squares[x].length; y++)
      Squares[x][y].display();

  stroke(0);
  fill(200);
  for (int i = 0; i < particles.length; i++)
    particles[i].update(Squares);
  for (int i = 0; i < particles.length; i++)
    particles[i].display();
}

void squareReset()
{
  float random_x = random(10000);
  float random_y = random(10000);

  Squares = null;
  Squares = new Square[32][18];
  for (int x = 0; x < Squares.length; x++)
    for (int y = 0; y < Squares[x].length; y++)
    {
      float vector = map(noise(random_x + x * 0.1, random_y + y * 0.1), 0, 1, 0, 4 * PI);
      Squares[x][y] = new Square(new PVector(x, y), width / Squares.length, vector);
    }

  Time = 0;
}

void particleReset()
{
  particles = null;
  particles = new Particle[100];
  for (int i = 0; i < particles.length; i++)
    particles[i] = new Particle(new PVector(random(width), random(height)), 20);
}

void keyReleased()
{
  particleReset();
}

Particle(流されてく粒子)クラス

class Particle
{
  PVector position;
  PVector vector;
  PVector acceleration;
  int radius;

  Particle(PVector position, int radius)
  {
    this.position = position;
    this.radius = radius;
    vector = new PVector(0, 0);
    acceleration = new PVector(0, 0);
  }

  void update(Square[][] squares)
  {
    int x = int(map(position.x, 0, width, 0, squares.length));
    int y = int(map(position.y, 0, height, 0, squares[0].length));

    acceleration = squares[x][y].getVector();
    acceleration.div(50);
    vector.add(acceleration);
    position.add(vector);

    if (position.x < width / squares.length)
    {
      position.x = width / squares.length;
      vector.x = 0;
    }
    if (position.x > width - width / squares.length)
    {
      position.x = width - width / squares.length;
      vector.x = 0;
    }
    if (position.y < height / squares[0].length)
    {
      position.y = height / squares[0].length;
      vector.y = 0;
    }
    if (position.y > height - height / squares[0].length)
    {
      position.y = height - height / squares[0].length;
      vector.y = 0;
    }
  }

  void display()
  {
    ellipse(position.x, position.y, radius, radius);
  }
}

Square(後ろ側のマス)クラス

class Square
{
  PVector position;
  float size;
  float angle;

  Square(PVector position, float size, float angle)
  {
    this.position = position;
    this.size     = size;
    this.angle   = angle;
  }

  PVector getVector()
  {
    return new PVector(cos(angle), sin(angle));
  }

  void display()
  {
    float center_x = position.x * size + size / 2;
    float center_y = position.y * size + size / 2;
    
    rect(center_x - size / 2, center_y - size / 2, size, size);
    line(center_x + cos(angle) * size / 4, center_y + sin(angle) * size / 4, center_x - cos(angle) * size / 4,          center_y - sin(angle) * size / 4);
    line(center_x + cos(angle) * size / 4, center_y + sin(angle) * size / 4, center_x + cos(angle + PI / 6) * size / 6, center_y + sin(angle + PI / 6) * size / 6);
    line(center_x + cos(angle) * size / 4, center_y + sin(angle) * size / 4, center_x + cos(angle - PI / 6) * size / 6, center_y + sin(angle - PI / 6) * size / 6);
  }
}