We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › constructing objects
Page Index Toggle Pages: 1
constructing objects (Read 1092 times)
constructing objects
Apr 3rd, 2010, 5:43am
 
Hi

how can i construct an object with a variable which is an array?

trying to construct a grid of cells (a cell is an object) which holds a value which is an array.
Would you construct this variable in the class constructor or in setup?

int numberOf = 3;
Cell [][] grid;
Cell [][] Limbo;

void setup() {
size(100,100);
//Cells
grid = new Cell[width][height];
Limbo = new Cell[width][height];
for (int i = 0; i < width; i ++ ) {
  for (int j = 0; j < height; j ++ ) {
    // Initialize each object
    grid[i][j] = new Cell(i, j);
    Limbo[i][j] = new Cell(i, j);
  }
}
}

class Cell{
int x, y;    
float [] variable = new float [numberOf];

Cell(int xpos, int ypos) {
  x = xpos;
  y = ypos;
}
}
Re: constructing objects
Reply #1 - Apr 3rd, 2010, 8:32am
 
I think you are doing it fine.
Re: constructing objects
Reply #2 - Apr 3rd, 2010, 8:47am
 
am i?

the code doesn't initialise the elements of the array "variable" in the Cell objects

i'm thinking that in setup the value of these elements need to be set to 0 - But i don't understand how to - whether this should be done in void setup() or in the class constructor.
Re: constructing objects
Reply #3 - Apr 3rd, 2010, 10:08am
 
ok, thinking about this a little more the values of the array 'variable' of Cell will default to 0.0.
is that correct?

if so then i understand PhiLho's comment
Re: constructing objects
Reply #4 - Apr 3rd, 2010, 10:53am
 
Yes, that's the default value of float numbers for class fields.
The new float instruction will be run for each cell you create.
Re: constructing objects
Reply #5 - Apr 3rd, 2010, 3:29pm
 


Hello,

I wrote a little more from your idea...
just for the fun of it...
You'll need PeasyCam.

Chrisir  Wink



Code:

// imports ================================================

/** opengl **/
// import processing.opengl.*;

/** PeasyCam **/
import peasy.*;

// vars =================================================

// Camera
PeasyCam cam;
CameraState MyState;
CameraState MyStateMove;


int numberOf = 3;
Cell [][] grid;
Cell [][] Limbo;

// program =======================================

void setup() {
// size(300,300,OPENGL);
size( 580, 480, P3D );

stroke (254);

cam = new PeasyCam(this, 400);
//cam.setMinimumDistance(250);
//cam.setMaximumDistance(600);
cam.pan(0,0);
cam.setDistance(1500);
cam.setResetOnDoubleClick (false);

//Cells
grid = new Cell[width][height];
Limbo = new Cell[width][height];
for (int i = 0; i < width; i ++ ) {
for (int j = 0; j < height; j ++ ) {
// Initialize each object
grid[i][j] = new Cell(i, j);
grid[i][j].x = i;
grid[i][j].y = j;
if (((i>100) && (i<200)) && ((j>100) && (j<200))) {
grid[i][j].variable[0] = 99;
}
else {
grid[i][j].variable[0] = 44;
}
Limbo[i][j] = new Cell(i, j);
Limbo[i][j].x = i;
Limbo[i][j].y = j;
}
}
}

void draw () {
background(0);
paintIt ();
}

void paintIt () {
for (int i = 0; i < width; i ++ ) {
for (int j = 0; j < height; j ++ ) {
// Initialize each object
grid[i][j].drawIt(); // = new Cell(i, j);
// Limbo[i][j].drawIt(); // = new Cell(i, j);
}
}
} // paintIt

class Cell{
int x, y;
float [] variable = new float [numberOf];

Cell(int xpos, int ypos) {
x = xpos;
y = ypos;
variable[0]=54;
variable[1]=14;
variable[2]=54;
}

void drawIt () {
stroke (color (variable[0],variable[1],variable[0]));
//point(x,y);
line (x,20,y,
x,20-variable[0],y);
}

} // class
Page Index Toggle Pages: 1