We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I'm a processing newbie so please forgive any misuse of terminology in trying to explain my question.
I can't quite get my head around the code that would allow multiple configurations of a 2D array to be nested in another array so that they could be individually called upon and drawn. For instance, in the code below I've commented out two additional array configurations that I'd like to be able to swap out with the one that loads at the end of setup.
Any help would be much appreciated.
Many thanks, Daniel
void setup() {
size(500, 300); // manually defined size based on intended grid
// define grid to be a 2D array
int[][] grid;
// specify size of the array in rows 1st and columns 2nd
grid = new int[3][5];
// set the dimensions of square grid units
int unitSize = 100;
// variable for the number of units in the grid rows
int rows = grid.length;
// variable for the number of units in the grid columns
int cols = grid[0].length;
// define palette
int white = 255;
int gray1 = 238;
int gray2 = 222;
int gray3 = 202;
int gray4 = 178;
int gray5 = 146;
int gray6 = 108;
int gray7 = 64;
int black = 0;
noStroke();
background(white);
// default grid (#1 of 3 possible)
// column 0
grid[0][0] = white;
grid[1][0] = gray3;
grid[2][0] = gray4;
// column 1
grid[0][1] = white;
grid[1][1] = black;
grid[2][1] = gray1;
// column 2
grid[0][2] = gray7;
grid[1][2] = gray1;
grid[2][2] = white;
// column 3
grid[0][3] = gray3;
grid[1][3] = gray1;
grid[2][3] = white;
// column 4
grid[0][4] = white;
grid[1][4] = gray6;
grid[2][4] = white; // end of initial grid (#1 of 3)
// // grid #2 of 3 possible
// // column 0
// grid[0][0] = gray2;
// grid[1][0] = gray2;
// grid[2][0] = white;
// // column 1
// grid[0][1] = gray2;
// grid[1][1] = gray5;
// grid[2][1] = gray5;
// // column 2
// grid[0][2] = white;
// grid[1][2] = gray2;
// grid[2][2] = gray2;
// // column 3
// grid[0][3] = white;
// grid[1][3] = gray2;
// grid[2][3] = white;
// // column 4
// grid[0][4] = gray2;
// grid[1][4] = gray2;
// grid[2][4] = white; // end grid (#2 of 3)
// // grid #3 of 3 possible
// // column 0
// grid[0][0] = white;
// grid[1][0] = white;
// grid[2][0] = gray3;
// // column 1
// grid[0][1] = white;
// grid[1][1] = gray3;
// grid[2][1] = gray3;
// // column 2
// grid[0][2] = white;
// grid[1][2] = gray3;
// grid[2][2] = gray3;
// // column 3
// grid[0][3] = white;
// grid[1][3] = gray3;
// grid[2][3] = black;
// // column 4
// grid[0][4] = white;
// grid[1][4] = gray3;
// grid[2][4] = gray4; // end grid (#3 of 3)
// draw initial grid
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
fill(grid[i][j]);
pushMatrix();
translate(j*unitSize, i*unitSize);
rect(0, 0, unitSize, unitSize);
popMatrix();
}
}
} // end setup()
void draw() {
} // end draw()
Answers
Sure, you can use an array of 2D arrays.
You could also use an ArrayList that holds 2D arrays.
You might also want to look at encapsulating your data inside an object instead of a bunch of arrays.
Which approach you take depends entirely on your context and what makes the most sense to you.
An alternative version:
I guess I've found some way -> a 3D array! (*)
Brilliant! I had a hunch about a 3D array but for the life of me I couldn't figure out how to implement it. This is fantastic... I'm going to dissect it line by line... and hopefully bend it to my will. Thank you so much. If I get stumped trying to comprehend something or have trouble bending it to my will is it ok if I post more questions to this thread? Thanks again!
Of course you can keep posting more questions! ^:)^
And btW, a slightly compacter version 3: ~:>
Excellent... thanks! I'm starting to more closely analyze your code now... wow.
Question: Is it possible for the palette arrays to reference rows instead of columns? My intention is to many more palette arrays and I think the code would be more visually logical if the rows of code corresponded to rows as they will be drawn. Granted I'm coming at this from a visual-centric perspective... and attempting to explore writing code as idiot proof and scalable as possible... since at this point my visual ideas greatly outweigh my programming ability.
Again, your help is greatly appreciated.
According to how the array's dimension order is laid out:
final color[][][] grids = new color[PALETTES][ROWS][COLS];
Right after the outer dimension comes the row dimension. Thus it's already in the way most of us like! :-j
What is dissonant is how you had laid out the gray scale in your original example!
Instead of defining row by row, you chose to define column by column:
So I've followed your lead, since I didn't know whether changing to row by row would confuse &/or displease you! /:)
But now that you asked for it, I've modified fillPalette() to accept a sequence or rows in order to fill the grid up:
http://studio.processingtogether.com/sp/pad/export/ro.9tBDwfhlJ2R9D/latest
I think I understand how that would seem dissonant from a code perspective. Guilty as charged... thanks for humoring me and for following my initial structure to reduce the chance of confusing me. Do you teach?
From a visual perspective I structured it that way as illustrated:
Having fillPalette() accept a sequence of rows in order to fill the grid up is likewise easier for me to visualize as compared to v3.01, if that makes any sense... so thank you!
From a programmer's perspective, row x col arrays are much easier than the opposite. Check my cropped image below:
Also, posted the code online:
http://studio.processingtogether.com/sp/pad/export/ro.9tBDwfhlJ2R9D/latest
That makes sense. I was looking at the code you posted and noticed the version changed. I've been monkeying around with v4.10 for a few hours this morning. Were the changes you made to v4.11 only comments?
Just comments like line #17 to warn it doesn't work in JavaScript mode.
Plus the header includes link for the online version!
In short, nothing has changed! ;)
Is there an advantage to using noLoop() in setup() and redraw() in keyPressed() as opposed to letting functions loop? Doesn't noLoop() need to be the last line in setup()? Is smooth() beneficial even if only rectangles will be drawn?
How would I go about making specific keys and/or mouse gestures move in different directions within the 3D array?
Thanks!
noLoop() turns off draw()'s auto-call. And thus sets the program in an eternal w8-for-user-reaction state.
Most obvious advantage is CPU & GPU saving! Another subtle 1 is that we control when canvas is updated w/ redraw()!
Nope! It merely sets looping variable to
false
. ;)Also, draw() is always called once after setup()! @-)
In this case, it doesn't seem so. Try out
smooth(2);
andsmooth(0);
and check whether render quality ever changes!I don't get what's exactly being moved? :-/
That all makes sense. Thank you.
Imagine that there are 25 (5 x 5) palette arrays instead of 3 and the middle one (#12) is the array initially drawn. Pressing the UP key would cause #7 to be drawn. From there pressing the RIGHT key would draw #8, then #9. From there you could only go UP to #4, LEFT to #8 or DOWN to #12. So the number of palette arrays would define the boundaries that could be navigated and thus drawn. Perhaps the corresponding mouse movements would function likewise. Does this make any sense?
So those palettes of grids are also grids themselves!? So the whole thing is 4D! @-)
Fortunately, there's a way to emulate a 2D behavior in a 1D array. So let's leave grids as 3D. B-)
But now, instead of constant PALETTES, we're gonna split it as PROWS & PCOLS instead.
final color[][][] grids = new color[PROWS*PCOLS][ROWS][COLS];
As you probably had noticed by now, variable idx represents the palette's current index. The grids' outer dimension.
Version 4.xx just circularly increased it every time the user hit a mouse or a key button.
This time you wish to specify 4 directions in order to navigate through palette's grids.
Horizontal movement is merely either decrease or increase idx. No much diff. on how it currently is.
Now for vertical movement, a whole row gotta be leaped over.
That translates as either decrease or increase idx by PCOLS value! *-:)
For those 2 diff. tasks, I've defined functions moveHorizontal() & moveVertical().
Check them out in new version 5.0 below. Online version's got updated as well: >:/
Actually, I've just realized there's no really a need to have separate moveHorizontal() & moveVertical() functions! :(|)
Well, unless you dislike allowing movements à la Pac-Man's maze, where it shows up at the opposite side! [-(
Anyways, renamed moveHorizontal() as jumpPalette(). And deleted moveVertical()! :-B
This is fantastic... thank you! I've been chewing on it for a few hours already. I have more questions now, of course... but I'm going to keep working on a new sketch that will at least make the questions more tangible... and perhaps some answers will reveal themselves by doing so.
removed duplicate posting...not sure how that happened...sorry
I've identified some questions and tried to resolve them but am stuck again... so I've pasted the new sketch below (with comments in the areas I'm struggling with) in the hopes that you can again point me in the right direction.
Here's what I'm trying to accomplish:
Continued thanks for your help.