We are about to switch to a new forum software. Until then we have removed the registration on this forum.
To a computer, arrays don't have any kind of a shape or dimension, they are just blocks of space where a value can be stored and retrieved. But most humans don't really think like this, it's usually easier to understand something if we can SEE it.
So, how do we "SEE" arrays?
Well, a regular, one dimensional array can be imagined like a numbered list of values stacked on top of eachother, and a two dimensional array might be seen as a chess board with numbered rows and numbered squares in each row...... but how do you imagine a three dimensional array? How about a four dimensional one?
This is a sketch I made to show how three dimensional arrays can be visualized in the real world. The idea is that there is a cube (which represents a three dimensional array) that has x layers, inside each layer are x rows, and in each row there are x values. In this particular sketch, I used a cube with four layers that have four rows and four values in those rows, so in all there are 64 unique values.
In this sketch, you use the number keys to choose a layer, row, or value, and you use BACKSPACE to go back a level:
// A three dimensional array (The cube)
// The fist "[]" represents the layer, while the second "[]" is the row in that layer and the
// third is the value in that row
String[][][] dim = {
{{"0a", "0b", "0c", "0d"},
{"0e", "0f", "0g", "0h"}, // Layer 0
{"0i", "0j", "0k", "0L"},
{"0m", "0n", "0o", "0p"}},
{{"1a", "1b", "1c", "1d"},
{"1e", "1f", "1g", "1h"}, // Layer 1
{"1i", "1j", "1k", "1L"},
{"1m", "1n", "1o", "1p"}},
{{"2a", "2b", "2c", "2d"},
{"2e", "2f", "2g", "2h"}, // Layer 2
{"2i", "2j", "2k", "2L"},
{"2m", "2n", "2o", "2p"}},
{{"3a", "3b", "3c", "3d"},
{"3e", "3f", "3g", "3h"}, // Layer 3
{"3i", "3j", "3k", "3L"},
{"3m", "3n", "3o", "3p"}}
};
int lay = -1; // The layer of the cube (cube 0)
int row = -1; // The row in that layer
int val = -1; // The value in that row
boolean show_startScreen = true;
void setup() {
size(500, 500);
rectMode(CENTER);
textAlign(CENTER, CENTER);
stroke(255);
textSize(30);
noFill();
noLoop(); // I don't need the program to run more than once unless a key is pressed
//and something is changed (so redraw will be run in "void keyPressed()")
}
void draw() {
background(0);
if (show_startScreen) { //if the start screen is still being shown, draw it!
text("Multidimensional Array\nVisualization Tool", 250, 100);
rect(250, 255, 350, 50);
text("Press any key to begin", 250, 250);
textSize(20);
text("Cyber_Agent\n:)", 250, 400); //The totally awesome guy who made it
textSize(30);
} else {
if (lay == -1) { // if a layer has not been chosen
text("Cube 0:", 250, 30);
text("Choose a layer", 250, 460);
rect(175, 175, 200, 200);
text("0", 250, 95);
rect(225, 225, 200, 200);
text("1", 300, 145);
rect(275, 275, 200, 200);
text("2", 350, 195);
rect(325, 325, 200, 200);
text("3", 400, 245);
} else if (row == -1) { // if a row has not been chosen
rect(250, 250, 400, 400);
text("Layer " + lay + ":", 250, 20);
text("Choose a row", 250, 475);
rect(250, 105, 350, 50);
text("0", 100, 100);
rect(250, 200, 350, 50);
text("1", 100, 195);
rect(250, 300, 350, 50);
text("2", 100, 295);
rect(250, 400, 350, 50);
text("3", 100, 395);
} else if (val == -1) { // if a value has not been chosen
text("Row " + row + ":", 250, 100);
text("Choose a value", 250, 400);
rect(100, 250, 100, 100);
text("0", 100, 250);
rect(200, 250, 100, 100);
text("1", 200, 250);
rect(300, 250, 100, 100);
text("2", 300, 250);
rect(400, 250, 100, 100);
text("3", 400, 250);
} else { //show a value
text("Value " + val + ":", 250, 100);
rect(250, 250, 100, 100);
text(dim[lay][row][val], 250, 250);
text("[" + lay + "]" + "[" + row + "]" + "[" + val + "]", 250, 400); //show the actual location/ address of your chosen value
}
}
}
void keyPressed() {
if (!show_startScreen) { // If the start screen has been passed
if (keyCode == BACKSPACE) { // go back a level
if (val != -1) {
val = -1;
} else if (row != -1) {
row = -1;
} else if (lay != -1) {
lay = -1;
}
} else if (key == '0' || key == '1'|| key == '2'|| key == '3') { //make sure that the pressed key was a 0, 1, 2, or 3
if (lay == -1) { // if layer has not been set, set it!
lay = Integer.parseInt(str(key)); //covert the pressed key to an int, and store it
} else if (row == -1) { // if layer has not been set, set it!
row = Integer.parseInt(str(key)); //covert the pressed key to an int, and store it
} else if (val == -1) { // if layer has not been set, set it!
val = Integer.parseInt(str(key)); //covert the pressed key to an int, and store it
}
}
} else {
show_startScreen = false;
}
key = 'q'; // This gets rid of some loopholes
redraw(); // Run the code in "draw()" once more
}
end of code
Honestly, this is so random and I don't think that this has any practical uses, but I am going to keep it handy just in case I suddenly forget how to wrap my head around three dimensional arrays.
P.S. A four dimensional array could be imagined as x cubes, each with x layers, and x rows in those layers, and x values in those rows. Perhaps a five dimensional array would be x groups of x cubes with x layers with x rows with x values. And so on....
Comments
Man, the code looked fine when I pasted it in, but it got all compressed after I posted it..... anyone know how I can fix that?
Edit post, highlight code, press ctrl-o
Oh! Thanks so much!
The fourth component can also be time.
E.g. the different moves in a 3D chess cube
here is a 3D cube with letters