'ArrayIndexOutOfBoundsException: 0' when initializing Voronoi

edited October 2017 in Library Questions

I'm trying to make a sketch that draws text as a Voronoi tessellation.

I used the geomerative library to get points around text. I was able to draw the points without a problem.

Next I tried to used the mesh library for voronoi. The points from geomerative are stored in an RPoint[] so I first moved those values into a 2D float array.

Finally, when trying to initialize the Vernoi class object, I get the error. I know this error usually means the array wasn't filled properly and there is no values stored in it. I ran prntln() to check that the array is in fact be filled.

Any help would be greatly appreciated. Thanks!

import megamu.mesh.*;
import geomerative.*;

RFont f;
RShape grp;
RPoint[] rPoints;
float[][] vPoints;
Voronoi txtVoronoi;

color bg = color(255);
int txtCol = 0;
int txtOp = 150;
int pointCol = 0;


void setup() {
  size(800, 600);

  //initialize geomerative library
  RG.init(this);

  // font file must be in data folder and must be ttf
  grp = RG.getText("VORONOI", "SFNSText.ttf", 200, CENTER);
}


void draw() {
  background(bg);

  translate(width/2, height/2);
  stroke(txtCol, txtOp);
  grp.draw();

  //RG.setPolygonizer(RG.UNIFORMLENGTH);
  int minSpace = 30;
  int maxSpace = 200;
  float spacing = lerp(minSpace, maxSpace, abs(sin(frameCount*0.005)));
  RG.setPolygonizerLength(spacing);
  rPoints = grp.getPoints();

  if (rPoints != null) {
    drawControlPoints(rPoints, 10);
    vPoints = convertArray(rPoints);

    Voronoi txtVoronoi = new Voronoi(vPoints);

    float[][] txtVEdges = txtVoronoi.getEdges();
    drawEdges(txtVEdges, 1);
  }
}

float[][] convertArray(RPoint[] rPointArray) {
  float[][] newArray = new float [rPointArray.length][2];
  for (int i=0; i < rPointArray.length; i++) {
    newArray[i][0] = rPointArray[i].x;
    newArray[i][1] = rPointArray[i].y;
  } 
  return newArray;
}

void drawControlPoints(RPoint[] points, int diam) {
  noFill();
  str(pointCol);
  for (int i=0; i < points.length; i++) {
    ellipse(points[i].x, points[i].y, diam, diam);
  }
}

void drawEdges(float[][] vEdges, int col) {
  switch(col) {
  case 0:      //black
    stroke(0);
    break;
  case 1:      //red
    stroke(255, 0, 0); 
    break;
  case 2:      //green
    stroke(0, 255, 0);
    break; 
  case 3:      //blue
    stroke(0, 0, 255);
    break;
  case 4:      //white
    stroke(255);
    break;
  }  

  for (int i=0; i<vEdges.length; i++) {
    float startX = vEdges[i][0];
    float startY = vEdges[i][1];
    float endX = vEdges[i][2];
    float endY = vEdges[i][3];
    line(startX, startY, endX, endY);
  }
}

Answers

Sign In or Register to comment.