Simple array question.
in
Programming Questions
•
2 years ago
Hi everyone, found this nice example for storing mouse movements into an array. There are however a couple of lines of code I don't get. Lines 10 - 12 move the values to the right but I just don't understand how. I just can't seem to put my finger on it, could anyone break the logic down a little more for me? Thanks in advance!
- int[] y; // Declare array
- void setup() {
- size(200, 100);
- y = new int[100]; // Create array
- }
- void draw() {
- background(204);
- // Shift the values to the right
- for (int i = y.length-1; i > 0; i--) {
- y[i] = y[i-1];
- }
- // Add new values to the beginning
- y[0] = constrain(mouseY, 0, height-1);
- // Display each pair of values as a line
- for (int i = 1; i < y.length; i++) {
- line(i, y[i], i-1, y[i-1]);
- }
- }
1