Skip to content

Instantly share code, notes, and snippets.

@sortofsleepy
Created July 1, 2023 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sortofsleepy/4dc9ad1e60505db0f0b905d3231ca77c to your computer and use it in GitHub Desktop.
Save sortofsleepy/4dc9ad1e60505db0f0b905d3231ca77c to your computer and use it in GitHub Desktop.
// 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