ArrayIndexOutOfBounds exception when using Mesh library
in
Contributed Library Questions
•
1 year ago
Hi,
I'm using Lee Byron's Mesh Library (
http://www.leebyron.com/else/mesh/) to create triangulations of source images. I am picking a random selection of pixels within the source image, and then using a Voronoi object to create the list of regions to draw. This works fine when I sample a smaller number of points, but when I increase the number of sample points to approximately 1500 or more, I get an exception when creating the Voronoi object:
Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 0
at megamu.mesh.IntArray.add(IntArray.java:22)
at megamu.mesh.Voronoi.<init>(Voronoi.java:127)
at MIF_delauney.setup(MIF_delauney.java:55)
at processing.core.PApplet.handleDraw(PApplet.java:1608)
at processing.core.PApplet.run(PApplet.java:1530)
at java.lang.Thread.run(Thread.java:680)
What is causing this exception? I can't figure out where I'm going out of bounds at all...
My code is as follows:
import megamu.mesh.*;
PImage img;
int[] delauney_points;
void setup() {
img = loadImage("newsobscura.jpeg");
size(img.width,img.height);
delauney_points = new int[0];
// pick N random points to sample
for (int i = 0; i < 3000; i++) {
delauney_points = (int[])append(delauney_points, floor(lerp(0, img.pixels.length, random(0,1) )));
}
float[][] points = new float[delauney_points.length][2];
for (int i=0; i<delauney_points.length; i++) {
points[i][0] = (delauney_points[i]%img.width);
points[i][1] = (delauney_points[i] - points[i][0])/img.width;
}
background(0);
stroke(255);
strokeWeight(2);
smooth();
img.loadPixels();
// create Voronoi object
Voronoi myVoronoi = new Voronoi( points );
// get regions
MPolygon[] myRegions = myVoronoi.getRegions();
for(int i=0; i<myRegions.length; i++)
{
// pick the colour of the cource pixel and use this colour to fill the shape
fill(red(img.pixels[delauney_points[i]]), green(img.pixels[delauney_points[i]]), blue(img.pixels[delauney_points[i]]));
myRegions[i].draw(this);
}
}
void draw() {
image(img, img.width, 0);
saveFrame("output.png");
}
1