Array Index Out Of Bounds Exception:0

edited August 8 in Using Processing

I am having a problem with the below code -

it is happening at line 72: This is the line that gets highlighted when the patch crashes.

Voronoi myVoronoi = new Voronoi( points );

Has anyone any ideas what the issue is? Thank you!

import megamu.mesh.*;

// Menu GUI. Bolleans to change visualizations
boolean debug = false;
boolean view = true;
boolean info=true;
boolean voronoi=false;
boolean lines=true;

FlowField flowfield; // Flowfield object
ArrayList<Vehicle> vehicles; // An ArrayList of vehicles
int nrParticles = 300; // number of elements/particles
float[][] points = new float[nrParticles][2]; // Array for the VORONOI cells

  void setup() {
  size(1280, 720);

  // Resolution of the flowfield. nr of cells
  flowfield = new FlowField(50);

  // create the elements in a random position
  vehicles = new ArrayList<Vehicle>();
  for (int i = 0; i < nrParticles; i++) {
    vehicles.add(new Vehicle(new PVector(random(width), random(height)), random(2, 15), random(0.1, 1.5)));
  }

  noCursor(); // hide the mouse

  // INICIATE VORONOY STUFF
  for (int i=0; i<nrParticles; i++) {
    points[i][0]= random(800);
    points[i][1]= random(800);
  }

  //Audiostuff      
  input = new AudioIn(this, 0);  //Create an Audio input and grab the 1st channel
  input.start();// start the Audio Input

  rms = new Amplitude(this); // create a new Amplitude analyze
  rms.input(input);  // Patch the input to an volume analyzer
  input.amp(1.0);

  smooth();
}

void draw() {

  //amplitude stuff
  float analise = map(rms.analyze(), 0, 0.5, 0.0, 50.0);
  audioIn+= (analise-audioIn)*0.01; //smoothing the audioIn vall

  background(0);

  flowfield.update(); // Flowfield update and display 
  if (debug) flowfield.display(); // If debug mode True, display flowfield 

  // Tell all the vehicles to follow the flow field
  for (Vehicle v : vehicles) {
    v.follow(flowfield);
    v.run();
  }

  // DRAWING VORONOI
  int nrVoronois=int(map(mouseY, 0, height, 0, nrParticles));

  //GETTING VEHICLES POSITION TO VORONOI'S POINTS
  for (int i=0; i<vehicles.size(); i++) {   
    points[i][0]= vehicles.get(i).location.x;
    points[i][1]= vehicles.get(i).location.y;
  }

  Voronoi myVoronoi = new Voronoi( points );
  MPolygon[] myRegions = myVoronoi.getRegions();

  for (int i=0; i<nrVoronois; i++)
  {
    // an array of points
    float[][] regionCoordinates = myRegions[i].getCoords();

    fill(int(map(i*255.0, 147, i*255.0/nrParticles, 130 * (i % 2), 255)));

     //fill(int(i*255.0/nrParticles, 130 * (i % 2), 225 * (i % 2)));

//fill(255,int(map(sum[i],0,10,255,0)));  // dar valor do FFT ao interior do voronoi
    if (voronoi) myRegions[i].draw(this); // draw this shape
  }

  float[][] myEdges = myVoronoi.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];
    stroke(255);
    if (lines) line( startX, startY, endX, endY  
  }
Tagged:

Answers

  • I am having some trouble running the library. First guess is to use random(width) instead of random(800); in lines 31/32. You could consider also checking the examples of voronoi available under the HE_mesh in tits geometry directory. Not sure if they are the same library or if they could work in your application.

    Also, I didn't see your implementation of the Vehicle class. Did you forget to included? It is great if you can post code that we can run.

    Kf

  • Hi Thanks for looking, I tried changing the random(800) to random(width). No joy. It still crashes with the above error. I will look at the HE_mesh examples now to see if it has any answers, and I have uploaded the patch for you to run with the vehicle class here -

    https://workupload.com/file/7NPLuMp

Sign In or Register to comment.