Skip to content

Commit 49bd439

Browse files
committed
altered to use modulo instead of moving the array down
1 parent 14efebb commit 49bd439

1 file changed

Lines changed: 13 additions & 15 deletions

File tree

content/examples/Basics/Input/StoringInput/StoringInput.pde

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,33 @@
66
* into an array and played back every frame. Between each
77
* frame, the newest value are added to the end of each array
88
* and the oldest value is deleted.
9+
*
10+
* Updated 27 February 2010.
911
*/
1012

1113
int num = 60;
1214
float mx[] = new float[num];
1315
float my[] = new float[num];
1416

15-
void setup()
16-
{
17+
void setup() {
1718
size(200, 200);
1819
smooth();
1920
noStroke();
2021
fill(255, 153);
2122
}
2223

23-
void draw()
24-
{
24+
void draw() {
2525
background(51);
2626

27-
// Reads throught the entire array
28-
// and shifts the values to the left
29-
for(int i=1; i<num; i++) {
30-
mx[i-1] = mx[i];
31-
my[i-1] = my[i];
32-
}
33-
// Add the new values to the end of the array
34-
mx[num-1] = mouseX;
35-
my[num-1] = mouseY;
27+
// Cycle through the array, using a different entry on each frame.
28+
// Using modulo (%) like this is faster than moving all the values over.
29+
int which = frameCount % num;
30+
mx[which] = mouseX;
31+
my[which] = mouseY;
3632

37-
for(int i=0; i<num; i++) {
38-
ellipse(mx[i], my[i], i/2, i/2);
33+
for (int i = 0; i < num; i++) {
34+
// which+1 is the smallest (the oldest in the array)
35+
int index = (which+1 + i) % num;
36+
ellipse(mx[index], my[index], i/2, i/2);
3937
}
4038
}

0 commit comments

Comments
 (0)