// Tutorial used: https://processing.org/examples/sinewave.html
int xspacing = 1;   // How far apart should each horizontal location be spaced
int w = 90;              // Width of entire wave
float theta = 0.0;  // Start angle at 0
float amplitude = 3.0;  // Height of wave
float period = 27.0;  // How many pixels before the wave repeats
float dx;  // Value for incrementing X, a function of period and xspacing
float[] yvalues;  // Using an array to store height values for the wave
float x;
float y;
float headheight = 80.0;
float eyesize = 12.0;
float bodyheight = 70.0;
float ghostwidth = 90.0;
float r, g, b;
int count = 0;
void setup() {
  size(200, 200);
  dx = (TWO_PI / period) * xspacing;
  yvalues = new float[w/xspacing];
}
void draw() {
  background(200);
  x = mouseX;
  y = mouseY;
  drawGhost();
  calcWave();
  renderWave();
  if(count++ % 20 == 0) {
    changeColor();
  }
}
void drawGhost() {
  float left = x - ghostwidth/2;
  stroke(r, g, b);
  fill(200);
  arc(x, y, ghostwidth, headheight, PI, 2*PI, OPEN);
  ellipse(x-20, y, eyesize, eyesize);
  ellipse(x+20, y+2, eyesize, eyesize);
  line(left, y, left, y+bodyheight);
  line(left+ghostwidth, y, left+ghostwidth, y+bodyheight);
}
void calcWave() {
  theta += 0.08;
  float x = theta;
  for (int i = 0; i < yvalues.length; i++) {
    yvalues[i] = sin(x)*amplitude;
    x+=dx;
  }
}
void renderWave() {
  float left = x - ghostwidth/2;
  float helpy = y+bodyheight+amplitude;
  stroke(r, g, b);
  line(left, helpy+yvalues[0], left, y+bodyheight); 
  noStroke();
  fill(r, g, b);
  for (int x = 0; x < yvalues.length; x++) {
    ellipse(left+1+x*xspacing, helpy+yvalues[x], 1, 1);
  }
  stroke(r, g, b);
  line(left+ghostwidth, helpy+yvalues[yvalues.length-1], 
  left+ghostwidth, y+bodyheight); 
}
void changeColor() {
  r = random(256);
  g = random(256);
  b = random(256);
}