Assigning colours to data in 2D array

Hello all,

For a geographical project I want to assign colours to data stored in a 2D array. I tried many things. I think I am just missing one small line of code, but I just can’t get my head around it. This is the code I wrote so far:


Cel[][] raster;
float[] data;

int cols = 300;
int rows = 300;

void setup() {
size(770, 950);
String[] numbers_I_dont_understand = loadStrings(“test_data.txt”);
data=float(split(numbers_I_dont_understand[0], ‘,’));

raster = new Cel[cols][rows];
for (int i = 0; i < 300; i++) {
for (int j = 0; j < 300; j++) {
raster[i][j] = new Cel(i2.5, j3, 2.44, 3);
println(raster[i][j]);
}
}
}

void draw() {
background(0);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
raster[i][j].display();
}
}
}

class Cel {
float x, y;
float w, h;
float c;

// Cel Constructor
Cel(float xpos, float ypos, float wdth, float hght) {
x = xpos;
y = ypos;
w = wdth;
h = hght;
}

void display() {
if (raster[i]>0){
fill(#88FF88);
} else {
fill(255);
}

noStroke();
rect(x, y, w, h);

}
}


The words “raster” and “cel” mean respectively “grid” and “cell” in Dutch. Also I think that it might be the issue that int cols & int rows are not communicating with the grid.

Thanks a bunch!

Well, there’s a problem in your Cel class’s display() function, right? Your single Cell should not access the raster arrays at all - the Cel itself should have the data it needs to know how to display itself.

I suggest you pass a value into the Cel when you create it. This means you will need to add a 5th parameter to the constructor function:

  Cel(float xpos, float ypos, float wdth, float hght, float ivalue) {

You will then need a new variable in the Cel class to store that value:

  float value;

And store it in the constructor:

  value = ivalue;

Next, work out where that data comes from! You are parsing a text file of data in setup() - is that the data you need to use? It looks like a 1D array of data. How will you map that 1D array of data to the 2D array of Cels?

Next, you will need to make use of the new value variable inside the class. Perhaps use it to determine the color of the rectangle you are drawing?

Thank you very much! You are so right. Also, your first question is spot-on (it just shows what kind of newbie I am). I will get on that now!

Ok, so I am very lost at this now…

I tried to convert the one dimensional array I got so far into a two dimensional array but it just completely failed on me. Every time it says that the elements of the array are not compatible (String is not compatible with float, String is not compatible with int, etc. etc.). Even now, it says that new_data has not been initialised?

//Setting up 1D array
int[]data;
String[] numbers_I_dont_understand = loadStrings(“test_data.txt”);
data=int(split(numbers_I_dont_understand[0], ‘,’));

//Initialising 2D array
int row=3;
int col=2;
int[][]new_data;
int i=0;

//Creating 2D array
for(int r=0;r<new_data.length;r++){
for(int c=0; c<new_data.length;c++){
new_data[c][r]=round(random(3));
}
}

printArray(new_data);

It might be useful if you could post a few lines from your test_data.txt file.

new_data is a 2D array. That’s what this line says:

int[][] new_data;

But that does not mean it exists! You have not created this array. You need to do this:

new_data = new int[col][row];

This makes spaces in that array for numbers.

The problem was that you were trying to put numbers into those spaces before there were spaces for those numbers.

Ah I see. Well, it still got there in the first code. What I got printed then:

[6771] 34.120453
[6772] 34.35722
[6773] 34.59399
[6774] 34.83076
[6775] 35.067787
[6776] 35.30658
[6777] 35.545372
[6778] 35.784164
[6779] 36.02296
[6780] 36.261753
[6781] 36.501923

(Yes, there are a lot of variables.)

So, I figured, what I have to do in the end is to add a fifth parameter to the Cel that assigns the data to every cell. So I guess it must be something like this:

  raster[i][j] = new Cel(i*2.5, j*3, 2.44, 3, value);

combined with:

class Cel {
float x, y;
float w, h;
float c;
float v;

// Cel Constructor
Cel(float xpos, float ypos, float wdth, float hght, float value) {
x = xpos;
y = ypos;
w = wdth;
h = hght;
v = value;
}
}