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();
}