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 & HelpOther Libraries › voronoi from mesh library
Page Index Toggle Pages: 1
voronoi from mesh library (Read 1683 times)
voronoi from mesh library
May 24th, 2010, 7:39pm
 
I'm a bit confused, here.  I'm working on a voronoi sketch. I've succeeded in adding new points and regenerating the voronoi, however, after adding several points I get an ArrayIndexOutOfBoundsException.  I can continue to add points (it doesn't halt the sketch) but I can't figure what causes the exception, or how to fix it.




Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
     at megamu.mesh.IntArray.add(IntArray.java:22)
     at megamu.mesh.Voronoi.<init>(Voronoi.java:127)
     at MeshLibDemo_3.initMesh(MeshLibDemo_3.java:68)
     at MeshLibDemo_3.mouseMoved(MeshLibDemo_3.java:95)
     at processing.core.PApplet.handleMouseEvent(Unknown Source)
     at processing.core.PApplet.checkMouseEvent(Unknown Source)
     at processing.core.PApplet.mouseMoved(Unknown Source)
     at java.awt.Component.processMouseMotionEvent(Component.java:6308)
     at java.awt.Component.processEvent(Component.java:6032)
     at java.awt.Container.processEvent(Container.java:2041)
     at java.awt.Component.dispatchEventImpl(Component.java:4630)
     at java.awt.Container.dispatchEventImpl(Container.java:2099)
     at java.awt.Component.dispatchEvent(Component.java:4460)
     at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269
)
     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174
)
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
     at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)



Quote:
import megamu.mesh.*;
Voronoi myVoronoi;
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(500,250);
  // initialize points and calculate diagrams
  initMesh();
  smooth();
}
void draw() {
  background(200);
  if(myRegions==null) return;
 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
   
  }  
noLoop();
}
}
void initMesh() {
  // is points array is null then initialize it
  if(points==null) initPoints();
  
  // 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 );
  myRegions = myVoronoi.getRegions();
  // 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() {
    points = new float[(int)random(5,30)][2];
  for(int i=0; i<points.length; i++) {
    points[i][0] = random(width); // first point, x
    points[i][1] = random(height); // first point, y
  }
}
void mouseMoved() {
  // if myRegions is null then mesh is not ready
  if(myRegions==null) return;
  points[0][0]=mouseX;
  points[0][1]=mouseY;
  initMesh();
}
 void mousePressed() {
     float[][] tmp = new float[1][2];
tmp[0][0]=mouseX;
  tmp[0][1]=mouseY;
  points = (float[][])concat( points, tmp );
  redraw();
 }


Re: voronoi from mesh library
Reply #1 - May 28th, 2010, 7:09am
 
I personally don't like the processing array features (although they do implement stuff like array resizing that is readily available in ruby). So I have no experience of using them....
For a pragmatic solution try the following:-
Quote:
void mousePressed() {
  float[][] tmp = new float[1][2];
  tmp[0][0]=mouseX;
  tmp[0][1]=mouseY;
  points[0] = tmp[0];
  redraw();
}

Smiley

I suspect that it is probably dodgy to resize arrays interfaced with external libraries (written in java). My solution seems to be more in line with Tom Cardens voronoicones example from processing hacks that your example reminds me of (worth a look if you haven't seen it).
Page Index Toggle Pages: 1