Hi everyone,
I wonder if someone can explaine to me how this code work :
int locX, locY;
int offsetX, offsetY;
final int unit = 32;
byte[][] terrain = new byte[10][10];
void setup() {
size(321, 321);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
terrain[i][j] = byte(ceil(noise(i*0.05,j*0.05)*6));
}
}
}
void draw() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
offsetX = i;
offsetY = j;
// Out of bounds
if (offsetX < 0 || offsetX > 9 || offsetY < 0 || offsetY > 9) fill(0xff808080); //grey
// Draw based on terrain
else {
if (terrain[offsetX][offsetY]>5){
fill(0xff005000); //dark green
}
else if (terrain[offsetX][offsetY]>4){
fill(0xff008000); //light green
}
else if (terrain[offsetX][offsetY]>3){
fill(0xffc0c000); //yellow
}
else {
fill(0xff0000c0); //blue
}
}
rect(i*unit, j*unit, unit, unit);
}
}
}
I understand (i think) that in the setup(), each “i” and “j” get a random number between “i” * 0.05 and “j” *0.05 and then multiply by 6.
And then in the draw(), it check if the values of terrain[offsetX][offsetY] is more than a number i choose and color the rectangles.
So i get that everything is based on : byte[][] terrain = new byte[10][10];
but i dont understand precisely how it’s work and how to controle the result.
ok so each rect get one random number based on his indexes. (ex: the top left rect get the number 4)
Then this number (4 in my example) is checked by the conditions and get a color.
and if i take the bottom right rect as an example, his index is 9,9. How the randomization work ?
if the rect indexes are 1,9 does it take a random number between 1 * 0.05 and 9 * 0.05 or does it work differently ? And so for the bottom right rect does it take a random number between 9 * 0.05 and 9 * 0.05 ?
it’s the randomization part of the code that i don’t understand completly
we retrieve the value at point i*0.05, j*0.05 (x,y) in this landscape.
The result is a constant between 0 and 1. We multiply it by 6 so it’s between 0 and 6 and then we use ceil() to round it up. So it’s an integer between 1 and 6.
But 1,2,3 are all blue:
else {
fill(0xff0000c0); //blue
}
No, it’s the random value at 9 * 0.05 and 9 * 0.05 in the 2D random landscape.
the resulting values are 1,2,3,4,5,6 or so.
here is a new code.
It saves the terrain (file gets overwritten every time !!!) and you can hit Space Bar to renew
int locX, locY;
int offsetX, offsetY;
final int unit = 32;
int[][] terrain = new int[10][10];
void setup() {
size(321, 321);
randomizeNoise();
}
void draw() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
offsetX = i;
offsetY = j;
// Draw based on terrain
if (terrain[offsetX][offsetY]>5) {
fill(0xff005000); //dark green
} else if (terrain[offsetX][offsetY]>4) {
fill(0xff008000); //light green
} else if (terrain[offsetX][offsetY]>3) {
fill(0xffc0c000); //yellow
} else {
fill(0xff0000c0); //blue
}
rect(i*unit, j*unit,
unit, unit);
fill(0);
text(terrain[i][j],
i*unit+4, j*unit + 17);
}
}
String[] s1=new String[10];
for (int i = 0; i < 10; i++) {
s1[i] = "";
for (int j = 0; j < 10; j++) {
s1[i] += terrain[i][j];
}
}
saveStrings( "TT.txt", s1);
//noLoop();
}
// ==============================================================
void keyPressed() {
// SPACE BAR or any key
randomizeNoise();
}
void randomizeNoise() {
noiseSeed( int(random(11111) ) );
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
terrain[i][j] = ceil(noise(i*0.05, j*0.05)*6);
}
}
}
//
You have to copy it from the folder of the previous sketch.
(the letters in the text file are upside down (or grid is rotated by 180°), but the terrains are the same in both Sketches. You can edit the text file in a text editor when you know what number signifies which type or terrain, but it’s rotated)
Chrisir
// THIS VERSION LOADS
int locX, locY;
int offsetX, offsetY;
final int unit = 32;
int[][] terrain = new int[10][10];
void setup() {
size(321, 321);
String[] stringsFromFile = loadStrings("TT.txt");
int x=0;
for (String s1 : stringsFromFile) {
for (int i=0; i < s1.length(); i++) {
terrain[x][i]= (int) (s1.charAt(i)-48);
}
x++;
}
//
}// func
void draw() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
offsetX = i;
offsetY = j;
// Draw based on terrain
if (terrain[offsetX][offsetY]>5) {
fill(0xff005000); //dark green
} else if (terrain[offsetX][offsetY]>4) {
fill(0xff008000); //light green
} else if (terrain[offsetX][offsetY]>3) {
fill(0xffc0c000); //yellow
} else {
fill(0xff0000c0); //blue
}
rect(i*unit, j*unit,
unit, unit);
fill(0);
text(terrain[i][j],
i*unit+4, j*unit + 17);
}
}
String[] s1=new String[10];
for (int i = 0; i < 10; i++) {
s1[i] = "";
for (int j = 0; j < 10; j++) {
s1[i] += terrain[i][j];
}
}
}
//