Created
July 1, 2023 13:49
-
-
Save sortofsleepy/4dc9ad1e60505db0f0b905d3231ca77c to your computer and use it in GitHub Desktop.
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
// Example of how to find indices to values in a 2D array that are below a specified index. | |
/* | |
Take the following | |
[0,1,2,3 | |
4,5,6,7 | |
8,9,10,11] | |
This formula helps you figure out, for instance, what the index of 4 is if we're looking at index 0. | |
This was done in Processing www.processing.org | |
*/ | |
int cols = 4; | |
int rows = 3; | |
ArrayList vals = new ArrayList(); | |
void setup(){ | |
for(int i = 0; i < cols; ++i){ | |
for(int j = 0; j < rows; ++j){ | |
vals.add(new PVector(random(10),random(10))); | |
} | |
} | |
println(vals); | |
println("\n"); | |
// the current index to look at | |
int currentIndex = 0; | |
// how far down the array we want to look at. | |
int howFarBelow = 1; | |
// get that index | |
int below = currentIndex + cols * howFarBelow; | |
// this should be the value that is below 0, or 4 in this case | |
println(vals.get(below)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment