oompa_l
Full Member
Offline
Posts: 212
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 {...}