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 › array and classes
Page Index Toggle Pages: 1
array and classes (Read 730 times)
array and classes
Mar 17th, 2010, 4:19pm
 
hi, i'm using processing since two weeks and have problems with putting a class into an array. it's not working. i think the structure is ok, but somewhere is a mistake.... i'm sitting since a week now and going from one error to the next....

this is the code:

Boid[] boidies = new Boid[150];       // amount of boids
int canvasX = 700;     // canvas size x-direction
int canvasY = 500;     // canvas size y-direction
int r = 0;             // color rgb: r-value
int g = 255;           // color rgb: g-value
int b = 255;           // color rgb: b-value
float cDist = 50;      // length of connecting lines
float inc = 0.001;     // move increasement every loop

float[] ptsX;  
float[] ptsY;
float[] posX;
float[] posY;

void setup() {
 size(canvasX, canvasY);
 smooth();
 stroke(0, 170, 250);
 strokeWeight(1);
 background(255);
 ellipseMode(CENTER);


 for (int i = 0; i < boidies.length; i++) {
   ptsX[i] = random(0, canvasX);
   ptsY[i] = random(0, canvasY);
   posX[i] = ptsX[i];
   posY[i] = ptsY[i];
 }
 
  for (int i = 0; i < boidies.length; i++) {
   boidies[0] = new Boid(posX[i],posY[i]);
 }
 
}


void draw() {
 background(0);
 fill(255);
 stroke(r, g, b, 200);
 strokeWeight(1);
 
for (int i = 0; i < boidies.length; i++) {
boidies[i].display();
boidies[i].connect();
boidies[i].move();
}
}

class Boid {
 
float[] ptsX;  
float[] ptsY;
float[] posX;
float[] posY;

Boid (posX, posY) {
   ptsX[i] = random(0, canvasX);
   ptsY[i] = random(0, canvasY);
   posX[i] = ptsX[i];
   posY[i] = ptsY[i];
   posX = posX[i];
   posY = posY[i];

}


void move() {
 // Update the boids' positions
 for (int i = 0; i < boidies.length; i++) {
   posX[i] = noise(ptsX[i]) * canvasX;
   posY[i] = noise(ptsY[i]) * canvasY;
   ptsX[i] = ptsX[i] + inc;
   ptsY[i] = ptsY[i] + inc;
 
 }
}

void connect() {
 // draw the connecting lines
 for (int i = 0; i < boidies.length-1; i++) {
   for (int j = i; j < boidies.length; j++) {
     if (dist(posX[j], posY[j], posX[i], posY[i]) < cDist) {
       line(posX[j], posY[j], posX[i], posY[i]);
     }
   }
 }
 }
 
void display() {
 // draw the boids (draw them last, so they are not masked by lines)
 for (int i = 0; i < boidies.length; i++) {
   ellipse(posX[i], posY[i], 4, 4);
 }
}
}


maybe someone has an idea??? thank you!!!!
Re: array and classes
Reply #1 - Mar 17th, 2010, 4:38pm
 
Tried to run this... your constructor needs the identifiers for posX, posY:

Boid (float posX, float posY)
Re: array and classes
Reply #2 - Mar 20th, 2010, 6:32pm
 
You have implemented two diferent ways of thinking into one, namely procedural and object oriented programming....you have a boid class but as it seems data in the class is meant to refer to all of the boids.

So: your Boid class holds arrays of coordinates. No need to. Each Boid must only refer to itself. The only part of it that refers to other boids is the function that checks the connections to others (refers to the boidies array).

Then just throw all these boids into an array and loop through them (as you did).

I changed your code and it works(nice one btw).

Code:
Boid[] boidies = new Boid[150];       // amount of boids 
int canvasX = 700;     // canvas size x-direction
int canvasY = 500;     // canvas size y-direction
int r = 0;             // color rgb: r-value
int g = 255;           // color rgb: g-value
int b = 255;           // color rgb: b-value
float cDist = 50;      // length of connecting lines
float inc = 0.001;     // move increasement every loop

void setup() {
 size(canvasX, canvasY);
 smooth();
 stroke(0, 170, 250);
 strokeWeight(1);
 background(255);
 ellipseMode(CENTER);

 for (int i = 0; i < boidies.length; i++) {
   boidies[i] = new Boid(random(0, canvasX),random(0, canvasY));
 }

}


void draw() {
 background(0);
 fill(255);
 stroke(r, g, b, 200);
 strokeWeight(1);

 for (int i = 0; i < boidies.length; i++) {
   boidies[i].display();
   boidies[i].connect();
   boidies[i].move();
 }
}

class Boid {

 float ptsX;  
 float ptsY;
 float posX;
 float posY;

 Boid (float posX, float posY) {
   ptsX = random(0, canvasX);
   ptsY = random(0, canvasY);
   posX = ptsX;
   posY = ptsY;

 }


 void move() {
   // Update the boids' positions
   for (int i = 0; i < boidies.length; i++) {
     posX = noise(ptsX) * canvasX;
     posY = noise(ptsY) * canvasY;
     ptsX = ptsX + inc;
     ptsY = ptsY + inc;

   }
 }

 void connect() {
   // draw the connecting lines
   for (int i = 0; i < boidies.length-1; i++) {
     Boid b=boidies[i];
       if (dist(posX, posY, b.posX, b.posY) < cDist) {
         line(posX, posY, b.posX, b.posY);
       }  
   }
 }

 void display() {
   // draw the boids (draw them last, so they are not masked by lines)
     ellipse(posX, posY, 4, 4);
 }
}
Re: array and classes
Reply #3 - Mar 22nd, 2010, 2:44pm
 
Thanks a lot! this was exactly what i needed! Smiley
Page Index Toggle Pages: 1