Skip to content

Instantly share code, notes, and snippets.

@shiffman
Created November 18, 2013 15:33
Show Gist options
  • Save shiffman/7529766 to your computer and use it in GitHub Desktop.
Save shiffman/7529766 to your computer and use it in GitHub Desktop.
Demonstration of averaging an array of values in Processing
// Average of values over time
// Daniel Shiffman
int[] values = new int[20];
void setup() {
size(300, 300);
}
void draw() {
background(0);
// Shift all values
for (int i = 0; i < values.length-1; i++) {
values[i] = values[i+1];
}
// Get the new value
values[values.length-1] = mouseX;
// Calculate average
float avg = 0;
for (int i = 0; i < values.length; i++) {
avg += values[i];
}
avg /= values.length;
// Draw raw value and average to compare
ellipse(mouseX, 100, 32, 32);
ellipse(avg, 200, 32, 32);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment