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 & HelpPrograms › how to call an array from another function
Page Index Toggle Pages: 1
how to call an array from another function (Read 1294 times)
how to call an array from another function
Aug 13th, 2008, 11:06pm
 
Hi
I am just learning processing but I wanted to plunge right in and learn a bit about re-using data arrays...my experiment starts with the flocking example, and I wanted to explore using the position of each boid as a point within an array that will drive a mesh - either voronoi, delauney, or hull. The library I am referring to is the mesh library that can be seen here : http://www.leebyron.com/else/mesh/

So what I am having trouble with is understanding how to transfer the array in the flocking example over to a mesh examples wherein:

 myVoronoi = new Voronoi( points );
 myHull = new Hull( points );
 myDelaunay = new Delaunay( points );

and points is a 2d array. In the flocking example:

class Flock {
 ArrayList boids; // An arraylist for all the boids

 Flock() {
   boids = new ArrayList(); // Initialize the arraylist
 }

AND

// We accumulate a new acceleration each time based on three rules
 void flock(ArrayList boids) {
   Vector3D sep = separate(boids);   // Separation
   Vector3D ali = align(boids);      // Alignment
   Vector3D coh = cohesion(boids);   // Cohesion
   // Arbitrarily weight these forces
   sep.mult(2.0f);
   ali.mult(1.0f);
   coh.mult(1.0f);
   // Add the force vectors to acceleration
   acc.add(sep);
   acc.add(ali);
   acc.add(coh);
 }

any advice? again, I'm just starting out, so what is probably totally straightforward is baffling me...
Re: how to call an array from another function
Reply #1 - Aug 14th, 2008, 12:26am
 
Quote:
I wanted to explore using the position of each boid as a point within an array that will drive a mesh - either voronoi, delauney, or hull.


what you need to build a mesh with this library is an array of float :

Voronoi myVoronoi = new Voronoi(points); where points is a float[][] array.

to do so, declare such an array, then iterate through the boids arraylist and retrieve their location's coordinates :

Quote:
float[][] points = new float[flock.boids.size()][2];

for (int i = 0; i < flock.boids.size(); i++) {
 Boid b = (Boid)flock.boids.get(i);
 Vector3D loc = b.loc;
 points[i][0] = loc.x;
 points[i][1] = loc.y;
}

Voronoi myVoronoi = new Voronoi(points);
Re: how to call an array from another function
Reply #2 - Aug 14th, 2008, 1:01am
 
thanks, though I got an error:

ArrayIndexOutofBoundsException:0

also - I am converting the position of each boid to a co-ordinate - wasn't this very co-ordinate constructed somewhere else in an array to feed each boid's position - in theory couldn't I call on this array instead of constructing a new one? i'm just trying to understand, but I'm sure your way is the right way Smiley
Re: how to call an array from another function
Reply #3 - Aug 14th, 2008, 1:47am
 
Quote:
thanks, though I got an error:

ArrayIndexOutofBoundsException:0

Should work (I've tested it with the Flocking example and it's working well). Please post your full code.

Quote:
also - I am converting the position of each boid to a co-ordinate - wasn't this very co-ordinate constructed somewhere else in an array to feed each boid's position

If you have a look on the Flocking example source code, well, the answer is no, there's no such array. Each boid's position is calculated according to all other boids' positions. And all these coordinates are stored independently into each Boid object. That's why you've got to create your own array and pass it to the mesh constructor.
Re: how to call an array from another function
Reply #4 - Aug 14th, 2008, 4:38am
 
I've tried putting the code you posted in different spots...my instinct, though is to put in the initPoints function since that's where there is similar code in an example i have for the mesh library. When I put the call "Voronoi myVoronoi = new Voronoi(points);" inside the initpoints I get the array error code I mentioned before. When I put it anywhere else I get a NullPointerException... anyways, here's what I got:

import megamu.mesh.*;

/**
* Flocking
* by Daniel Shiffman.  
*
* An implementation of Craig Reynold's Boids program to simulate
* the flocking behavior of birds. Each boid steers itself based on
* rules of avoidance, alignment, and coherence.
*
* Click the mouse to add a new boid.
*/

Flock flock;
Voronoi myVoronoi;
Delaunay myDelaunay;
Hull myHull;

float[][] points;
float[][] myEdges;
MPolygon myRegions[],myHullRegion;
int col[];

float startX,startY,endX,endY;
float[][] regionCoordinates;

boolean showVoronoi=true;
boolean showDelaunay=false;
boolean showHull=false;

void setup() {
 size(200,200);
 colorMode(RGB,255,255,255,100);
 flock = new Flock();
 // Add an initial set of boids into the system
 for (int i = 0; i < 50; i++) {
   flock.addBoid(new Boid(new Vector3D(width/2,height/2),2.0f,0.05f));
 
 initMesh();
 smooth();
 
 
}

void draw() {
 background(100);
 flock.run();
 
}

// Add a new boid into the System
void mousePressed() {
 flock.addBoid(new Boid(new Vector3D(mouseX,mouseY),2.0f,0.05f));
}

void initMesh() {
 // is points array is null then initialize it
 //if(points==null) initPoints();
 
Voronoi myVoronoi = new Voronoi(points);

 // save the current number of regions, so that
 // we can check if it's the same after the Voronoi
 // has been recalculated.
 //int oldlength=0;
 //if(myRegions!=null) oldlength=myRegions.length;
 
 //myVoronoi = new Voronoi( points );
 //myHull = new Hull( points );
 //myDelaunay = new Delaunay( points );

 //myRegions = myVoronoi.getRegions();
 //myHullRegion = myHull.getRegion();
 //myEdges = myDelaunay.getEdges();

 // if the number of regions is different than
 // before then recalculate the random colors
/*
 if(oldlength!=myRegions.length) {
   col=new int[myRegions.length];
   for(int i=0; i<myRegions.length; i++) {
     float prob=random(100);
     if(prob>60) col[i]=color(random(30,100));    
     else col[i]=color(random(200,255));  
   }
   col[0]=color(255,0,0);
 }*/
}

void initPoints() {
   float[][] points = new float[flock.boids.size()][2];

   for (int i = 0; i < flock.boids.size(); i++) {
   Boid b = (Boid)flock.boids.get(i);
   Vector3D loc = b.loc;
   points[i][0] = loc.x;
   points[i][1] = loc.y;
 }
 
 

}

class Flock {.....
Re: how to call an array from another function
Reply #5 - Aug 14th, 2008, 2:19pm
 
Just add it into the draw() method instad. That will run with no error.

Code:
import megamu.mesh.*;

Flock flock;

void setup() {
size(200,200);
colorMode(RGB,255,255,255,100);
flock = new Flock();
// Add an initial set of boids into the system
for (int i = 0; i < 50; i++) {
flock.addBoid(new Boid(new Vector3D(width/2,height/2),2.0f,0.05f));
}
smooth();
}

void draw() {
background(100);
flock.run();
createAndDisplayMesh();
}

void createAndDisplayMesh() {

// create
float[][] points = new float[flock.boids.size()][2];
for (int i = 0; i < flock.boids.size(); i++) {
Boid b = (Boid)flock.boids.get(i);
Vector3D loc = b.loc;
points[i][0] = loc.x;
points[i][1] = loc.y;
}
Voronoi myVoronoi = new Voronoi(points);

// display
MPolygon[] myRegions = myVoronoi.getRegions();
fill(255,0,0);
for(int i=0; i<myRegions.length; i++) {
float[][] regionCoordinates = myRegions[i].getCoords();
myRegions[i].draw(this);
}

}

class Flock { ... }

class Boid { ... }


From: Flocking example source code.
Re: how to call an array from another function
Reply #6 - Aug 14th, 2008, 10:47pm
 
thanks for your help...it's great. but one more thing - say I want to toggle between Delauney, Voronoi and Convex hull as in the mesh example how would that work - here's me trying to implement it based on the example and I think all three are visible at one time - I want to toggle each ones visibility on and off and even reveal the underlying boids...

import megamu.mesh.*;

/**
* spaceMakingAgents
* by Gabriel Friedman.  
*
* Cross breesing Daniel Shis=fman's flocking simulation and Lee Byron's Mesh library...
* "An implementation of Craig Reynold's Boids program to simulate
* the flocking behavior of birds. Each boid steers itself based on
* rules of avoidance, alignment, and coherence."
*
* Click the mouse to add a new boid.
*/

Flock flock;

boolean showVoronoi=true;
boolean showDelaunay=false;
boolean showHull=false;

Voronoi myVoronoi;
Delaunay myDelaunay;
Hull myHull;

float startX,startY,endX,endY;
float[][] regionCoordinates;

float[][] points;
float[][] myEdges;
MPolygon myRegions[],myHullRegion;
int col[];

void setup() {
 size(600,400);
 colorMode(RGB,255,255,255,100);
 flock = new Flock();
 // Add an initial set of boids into the system
 for (int i = 0; i < 50; i++) {
   flock.addBoid(new Boid(new Vector3D(width/2,height/2),2.0f,0.05f));
 }
 smooth();
}

void draw() {
 background(100);
 flock.run();
 createAndDisplayMesh();
 
  // draw Voronoi
 if(showVoronoi) {
   strokeWeight(1);
   stroke(0);
 }
 
 // draw Voronoi as lines
 if(showDelaunay) {
   strokeWeight(2);
   stroke(255,0,0);
 }
 
 // draw Hull in semi-transparent yellow
 if(showHull) {
   strokeWeight(1);
   stroke(0);
   fill(255,255,0, 150);
 
 }
}

void keyPressed() {
 // reset points and mesh when spacebar is pressed
 /*if(key==' ') {
   initPoints();
   initMesh();
 }
 */
 // use keys '1'-'3' to toggle display
 if(key=='1') showVoronoi=!showVoronoi;
 if(key=='2') showDelaunay=!showDelaunay;
 if(key=='3') showHull=!showHull;
}
void createAndDisplayMesh() {
 
 // create
 float[][] points = new float[flock.boids.size()][2];
 for (int i = 0; i < flock.boids.size(); i++) {
   Boid b = (Boid)flock.boids.get(i);
   Vector3D loc = b.loc;
   points[i][0] = loc.x;
   points[i][1] = loc.y;
 }
 Voronoi myVoronoi = new Voronoi(points);
 Delaunay myDelaunay = new Delaunay( points );
 Hull myHull = new Hull( points );
 
 
 // display Voronoi
 MPolygon[] myRegions = myVoronoi.getRegions();
 fill(0,0,0);
 for(int i=0; i<myRegions.length; i++) {
   float[][] regionCoordinates = myRegions[i].getCoords();
   myRegions[i].draw(this);
 }

 //display Delauney
 float[][] myEdges = myDelaunay.getEdges();

 for(int i=0; i<myEdges.length; i++)
 {
float startX = myEdges[i][0];
float startY = myEdges[i][1];
float endX = myEdges[i][2];
float endY = myEdges[i][3];
line( startX, startY, endX, endY );
 }
 
 //display Hull
 MPolygon myRegion = myHull.getRegion();
 fill(255,0,0);
 myRegion.draw(this);
 
}


class Flock {...}


class Boid {...}
Re: how to call an array from another function
Reply #7 - Aug 17th, 2008, 5:57pm
 
Use a switch.
http://processing.org/reference/switch_.html
Re: how to call an array from another function
Reply #8 - Aug 26th, 2008, 8:26pm
 
is that what's happening in the following code:

void keyPressed() {
 // reset points and mesh when spacebar is pressed
 if(key==' ') {
   initPoints();
   initMesh();
 }
 
 // use keys '1'-'3' to toggle display
 if(key=='1') showVoronoi=!showVoronoi;
 if(key=='2') showDelaunay=!showDelaunay;
 if(key=='3') showHull=!showHull;
}

and THEN:

void draw() {
 background(200);
 if(myRegions==null) return;

 // draw Voronoi
 if(showVoronoi) {
   strokeWeight(1);
   stroke(0);
   for(int i=0; i<myRegions.length; i++) {
     fill(col[i]); // use random color for each region
     regionCoordinates = myRegions[i].getCoords();
     myRegions[i].draw(this); // draw this shape
   }
 }
 
 // draw Voronoi as lines
 if(showDelaunay) {
   strokeWeight(2);
   stroke(255,0,0);
   for(int i=0; i<myEdges.length; i++) {
     startX = myEdges[i][0];
     startY = myEdges[i][1];
     endX = myEdges[i][2];
     endY = myEdges[i][3];
     line(startX, startY, endX, endY);
   }
 }
 
 // draw Hull in semi-transparent yellow
 if(showHull) {
   strokeWeight(1);
   stroke(0);
   fill(255,255,0, 150);
   myHullRegion.draw(this);
 }
}
Page Index Toggle Pages: 1