Home

Video: Array utilities

Code for last example

int[] xs;
int[] ys;

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

  xs = new int[20];
  ys = new int[20];

  for (int i = 0; i < 20; i++)
  {
    xs[i] = int(random(0, 500));
    ys[i] = int(random(0, 500));
  }
}

void draw()
{
  background(0);
  for (int i = 0; i < xs.length; i++)
  {
    fill(255);
    rect(xs[i], ys[i], 10, 10);
  }
}

void keyPressed()
{
  if (key == CODED)
  {
    if (keyCode == UP)
    {
      xs = expand(xs, xs.length + 1);
      ys = expand(ys, ys.length + 1);
      xs[xs.length - 1] = int(random(0, 500));
      ys[ys.length - 1] = int(random(0, 500));
    }
    if (keyCode == DOWN)
    {
      if(xs.length > 0) {
        xs = shorten(xs);
        ys = shorten(ys);
      }
    }
  }
}
CSE 1211 material by Joshua Eckroth is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Source code for this website available at GitHub.