Mouse Signals.

Mouse Signals.

Move and click the mouse to generate signals. The top row is the signal from “mouseX”, the middle row is the signal from “mouseY”, and the bottom row is the signal from “mousePressed”.

 
int[] xvals;
int[] yvals;
int[] bvals;

void setup() {
  size(640, 360);
  noSmooth();
  xvals = new int[width];
  yvals = new int[width];
  bvals = new int[width];
}

int arrayindex = 0;

void draw() {
  background(102);
  
  for (int i = 1; i < width; i++) { 
    xvals[i-1] = xvals[i]; 
    yvals[i-1] = yvals[i];
    bvals[i-1] = bvals[i];
  } 
  // Add the new values to the end of the array 
  xvals[width-1] = mouseX; 
  yvals[width-1] = mouseY;
  
  if (mousePressed == true) {
    bvals[width-1] = 0;
  } else {
    bvals[width-1] = height/3;
  }
  
  fill(255);
  noStroke();
  rect(0, height/3, width, height/3+1);

  for(int i = 1; i < width; i++) {
    // Draw the x-values
    stroke(255);
    point(i, map(xvals[i], 0, width, 0, height/3-1));
    
    // Draw the y-values
    stroke(0);
    point(i, height/3+yvals[i]/3);
    
    // Draw the mouse presses
    stroke(255);
    line(i, (2*height/3) + bvals[i], i, (2*height/3) + bvals[i-1]);
  }
}

Functions Used

fill()

Sets the color used to fill shapes

Learn More
stroke()

Sets the color used to draw lines and borders around shapes

Learn More
size()

Defines the dimension of the display window width and height in units of pixels

Learn More
rect()

Draws a rectangle to the screen

Learn More
setup()

The setup() function is run once, when the program starts

Learn More
point()

Draws a point, a coordinate in space at the dimension of one pixel

Learn More
noSmooth()

Draws all geometry and fonts with jagged (aliased) edges and images when hard edges between the pixels when enlarged rather than interpoloating pixels

Learn More
map()

Re-maps a number from one range to another

Learn More
draw()

Called directly after setup(), the draw() function continuously executes the lines of code contained inside its block until the program is stopped or noLoop() is called

Learn More
for

Controls a sequence of repetitions

Learn More
noStroke()

Disables drawing the stroke (outline)

Learn More
line()

Draws a line (a direct path between two points) to the screen

Learn More
background()

The background() function sets the color used for the background of the Processing window

Learn More