# Exception in Toxiclibs voronoi.addPoint() when image is large (5k x 5k or larger)

**URL:** https://discourse.processing.org/t/exception-in-toxiclibs-voronoi-addpoint-when-image-is-large-5k-x-5k-or-larger/29541
**Category:** Libraries
**Created:** [April 21, 2021, 11:19am UTC](https://discourse.processing.org/t/exception-in-toxiclibs-voronoi-addpoint-when-image-is-large-5k-x-5k-or-larger/29541 "2021-04-21T11:19:14Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mocelet](https://avatars.discourse-cdn.com/v4/letter/m/85e7bf/32.png) [@mocelet](https://discourse.processing.org/u/mocelet)
#### Post date: [April 21, 2021, 11:19am UTC](https://discourse.processing.org/t/exception-in-toxiclibs-voronoi-addpoint-when-image-is-large-5k-x-5k-or-larger/29541/1 "2021-04-21T11:19:14Z")

</div>

I am using the Toxiclibs voronoi mesh to generate a set of non-overlapping lines in a circle, and have based my sketch on the example code I found at [https://forum.processing.org/one/topic/toxiclib-voronoi-example-sketch.html](https://forum.processing.org/one/topic/toxiclib-voronoi-example-sketch.html)

For smaller image sizes (3000x3000 and smaller) the code below is working as intended, but on larger images (5000x5000 and larger) I sometimes get an exception when voronoi.addPoint() is called:

```auto
Warning: Checking all triangles for DelaunayVertex(4802.423828125,1680.7874755859375)
Warning: No triangle holds DelaunayVertex(4802.423828125,1680.7874755859375)
IllegalArgumentException: No containing triangle
IllegalArgumentException: No containing triangle
IllegalArgumentException: No containing triangle

```

In the saved image I see all the points, but the mesh between the points is not present in the lower right part of the image. As the image size increases, the size of the region without mesh also increases (10% size image, let me know if you want to see the full size image):

![Points756_20210421-120600 10%](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/bc8ea4483cbcb52facad05d477ac8ccb6e217899.png)

My goal is to have a 30000 x 30000 image, with the lines evenly distributed across the circle, so any help would be greatly appreciated.

The full sketch is:

```auto
import toxi.geom.*;
import toxi.geom.mesh2d.*;
import toxi.util.*;
import toxi.util.datatypes.*;
import toxi.processing.*;

// ranges for x/y positions of points
FloatRange xpos, ypos;

// helper class for rendering
ToxiclibsSupport gfx;

// empty voronoi mesh container
Voronoi voronoi = new Voronoi();

// switches
boolean doShowPoints = true;
boolean doShowHelp = false;
boolean doSave = true;

int seed=12345;
int imageDim = 5000;
int numPoints = 0;

void settings(){
  size(imageDim, imageDim);
}  

void setup() {
  randomSeed(seed);
  smooth();

  xpos=new FloatRange(0, width);
  ypos=new FloatRange(0, height); 
  
  gfx = new ToxiclibsSupport(this);
  textFont(createFont("SansSerif", 10));
  
  addRandomPoints(1000);
}

void draw() {
  //White background
  background(255);
  
  //Background circle
  fill (170, 170, 170);
  ellipse(width*0.5, height*0.5,width, height);
  
  // draw all voronoi polygons, clip them if needed...
  stroke(0);
  noFill();
  for(Polygon2D tri : voronoi.getRegions()) {
    java.util.List<Vec2D> vert = tri.vertices;
    
    for(int i=0; i<vert.size(); i++){
      Vec2D p1=vert.get(i);
      Vec2D p2=vert.get((i+1) % vert.size());
      
      Vec2D start=p1.interpolateTo(p2,0.1);
      Vec2D end=p1.interpolateTo(p2,0.9);
      
      if(onCircle(start) && onCircle(end)){
        gfx.line(start,end);
      }
    }
  }
  
  // draw original points added to voronoi
  if (doShowPoints) {
    fill(255, 0, 255);
    noStroke();
    for (Vec2D c : voronoi.getSites()) {
      ellipse(c.x, c.y, 5, 5);
    }
  }
  
  if (doSave) {
    saveFrame("Points" + numPoints + "_" + DateUtils.timeStamp() + ".tif");
    doSave = false;
  }
}

void keyPressed() {
  switch(key) {
  case ' ':
    doSave = true;
    break;
  case 'x':
    voronoi = new Voronoi();
    break;
  case 'p':
    doShowPoints = !doShowPoints;
    break;
  case 'h':
    doShowHelp=!doShowHelp;
    break;
  case 'r':
    addRandomPoints(100);
    break;
  }
}

void addRandomPoints(int count){
  Vec2D p = new Vec2D(0,0);
  for (int i = 0; i < count; i++) {
      p.set(xpos.pickRandom(), ypos.pickRandom());
    
    try {
      voronoi.addPoint(p);
    }
    catch(Exception e) {
      numPoints--;
    }
  }
  numPoints += count;
}

boolean onCircle(Vec2D a) {
  Vec2D c = new Vec2D(width/2, height/2);
  float maxRadPx = width/2;
 
  return (a.distanceTo(c) <= maxRadPx);
}

```

---

<div class="post-metadata">

### Author: ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)
#### Post date: [April 21, 2021, 12:36pm UTC](https://discourse.processing.org/t/exception-in-toxiclibs-voronoi-addpoint-when-image-is-large-5k-x-5k-or-larger/29541/2 "2021-04-21T12:36:35Z")

</div>

Often a Delaunay Triangulation (from which a Voronoi diagram is computed) is initialised with a large triangle.

Indeed if we look at the [source code](https://github.com/postspectacular/toxiclibs/blob/6896bc17e85320e886bdf0819c66491ce484728e/src.core/toxi/geom/mesh2d/Voronoi.java#L52) we see this is the case:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/eac61032c2654109c5b03dd1f19ba3e6e30c1411.png)

ToxicLibs doesn’t seem to support adding a vertex that lies outside this initial triangle.

Not quite to scale, but this show’s going on (remember in Processing the y-axis is flipped):

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/6bf417e4a316e59ba02bfcd2559793e2fa542259.jpeg)

Lucky for you the `DEFAULT_SIZE` size field is `public`, so you can increase easily it to support a larger diagram.

```auto
Voronoi.DEFAULT_SIZE = 30000;
voronoi = new Voronoi();

```

---

<div class="post-metadata">

### Author: ![mocelet](https://avatars.discourse-cdn.com/v4/letter/m/85e7bf/32.png) [@mocelet](https://discourse.processing.org/u/mocelet)
#### Post date: [April 21, 2021, 1:02pm UTC](https://discourse.processing.org/t/exception-in-toxiclibs-voronoi-addpoint-when-image-is-large-5k-x-5k-or-larger/29541/3 "2021-04-21T13:02:08Z")

</div>

You are a star. Thank you.

I couldn’t get it to recognise .DEFAULT\_SIZE as something it could set, but passing a larger initial size to the constructor fixes the problem.
