Created
November 18, 2013 15:33
-
-
Save shiffman/7529766 to your computer and use it in GitHub Desktop.
Demonstration of averaging an array of values in Processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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