# Mesh – A Processing Library

Mesh is a library for creating Voronoi, Delaunay and Convex Hull diagrams in Processing. After searching online for a Java package for creating Voronoi diagrams and failing to find anything simple enough to fit my needs I decided to make my own as simple as possible. I did find the wonderfully useful QuickHull3D package, which the algorithms for creating these diagrams are based on. These complete in O(n log n) time.

# Voronoi Diagram Voronoi Diagrams show the regions of space closest to each point. This can be used to create interesting visual patterns or as a preprocess for determining which point is closest to any given place in space. This is particulary useful for optimization in game design.

new Voronoi( float[][] points )

This creates and returns a new Voronoi object, which you can later ask for edges and regions. It takes a two dimensional array for describing points, you can think of this as an array of points.

```float[][] points = new float;

points = 120; // first point, x
points = 230; // first point, y
points = 150; // second point, x
points = 105; // second point, y
points = 320; // third point, x
points = 113; // third point, y

Voronoi myVoronoi = new Voronoi( points );```

getRegions()

`getRegions()` returns an array of MPolygons, the order of which correspond to the order of the points entered. MPolygon contains the points of the polygon, and can be drawn to the stage.

```MPolygon[] myRegions = myVoronoi.getRegions();

for(int i=0; i<myRegions.length; i++)
{
// an array of points
float[][] regionCoordinates = myRegions[i].getCoords();

fill(255,0,0);
myRegions[i].draw(this); // draw this shape
}```

getEdges()

`getEdges()` returns a two dimensional array, or an array of lines. Use this to draw stroke lines on top of the regions.

```float[][] myEdges = myVoronoi.getEdges();

for(int i=0; i<myEdges.length; i++)
{
float startX = myEdges[i];
float startY = myEdges[i];
float endX = myEdges[i];
float endY = myEdges[i];
line( startX, startY, endX, endY );
}```

# Delaunay Diagram A Delaunay Diagram is an optimal triangulation of a given set of points. This is very helpful for applying mesh textures, or creating structures for physical simulation. Voronoi and Delaunay Diagrams are duals. For every edge of a Delaunay diagram, an edge of a Voronoi diagram perpendicularly bisects it.

new Delaunay( float[][] points )

This creates and returns a new Delaunay object, which you can later ask for edges and links. It takes a two dimensional array for describing points, you can think of this as an array of points.

```float[][] points = new float;

points = 120; // first point, x
points = 230; // first point, y
points = 150; // second point, x
points = 105; // second point, y
points = 320; // third point, x
points = 113; // third point, y

Delaunay myDelaunay = new Delaunay( points );```

getEdges()

`getEdges()` returns a two dimensional array, or an array of lines. Use this to draw lines for connections.

```float[][] myEdges = myDelaunay.getEdges();

for(int i=0; i<myEdges.length; i++)
{
float startX = myEdges[i];
float startY = myEdges[i];
float endX = myEdges[i];
float endY = myEdges[i];
line( startX, startY, endX, endY );
}```

`getLinks()` returns a two dimensional array, or an array of links. This is similar to `getEdges()` however rather than returning the actual coordinates of the edges, it returns integers which are indexes to the points array used to create the Delaunay object. This can be useful when the links themselves are important, such as when building springs in a particle system.

```int[][] myLinks = myDelaunay.getLinks();

{

float startX = points[startIndex];
float startY = points[startIndex];
float endX = points[endIndex];
float endY = points[endIndex];
line( startX, startY, endX, endY );
}```

Similarily, `getLinked( int index )` returns indexes to the original points array. It takes an index and returns those indexes which are linked to it in the Delaunay diagram.

```int[] localLinks = myDelaunay.getLinked(0); // those linked to point #1

// outputs:
//  2
//  1```

# Convex Hull A Convex Hull is the encompassing shape around a group of points. As if you were to wrap a piece of string around all of the points. This is handy when doing collision tests on complex shapes, or finding the most extreme points within a dataset.

new Hull( float[][] points )

This creates and returns a new Hull object, which you can later ask for region and extrema. It takes a two dimensional array for describing points, you can think of this as an array of points.

```float[][] points = new float;

points = 120; // first point, x
points = 230; // first point, y
points = 150; // second point, x
points = 105; // second point, y
points = 320; // third point, x
points = 113; // third point, y

Hull myHull = new Hull( points );```

getRegion()

`getRegion()` returns an MPolygon of the Convex Hull. MPolygon contains the points of the polygon, and can be drawn to the stage.

```MPolygon myRegion = myHull.getRegion();
fill(255,0,0);
myRegion.draw(this);```

getExtrema()

`getExtrema()` returns an array of integers which are indexes to the original points array. These points are extrema within the set of points and are the vertices of the hull. The order of which are clockwise from the topmost point.

```int[] extrema = myHull.getExtrema();
println( extrema );

// outputs:
//  1
//  2
//  0```