Skip to content

Instantly share code, notes, and snippets.

@sabotai
Created April 29, 2017 00:09
Show Gist options
  • Save sabotai/83ade43abeffcce5c7341a4e7b8ee32c to your computer and use it in GitHub Desktop.
Save sabotai/83ade43abeffcce5c7341a4e7b8ee32c to your computer and use it in GitHub Desktop.
Testing Voronoi Patterns using Mesh Library
import megamu.mesh.*;
int numPoints = 20;
float[][] points = new float[numPoints][2];
PVector[] vPoints = new PVector[numPoints];
float[] xSpeed = new float[numPoints];
float[] ySpeed = new float[numPoints];
Voronoi myVoronoi;
Delaunay myDelaunay;
float[][] myEdges;
float[][] dEdges;
int[][] myLinks;
void setup() {
size(1920, 1080);
//points[0][0] = 120; // first point, x
//points[0][1] = 230; // first point, y
//points[1][0] = 150; // second point, x
//points[1][1] = 105; // second point, y
//points[2][0] = 320; // third point, x
//points[2][1] = 113; // third point, y
for (int i = 0; i < numPoints; i++) {
points[i][0] = random(0, width);
points[i][1] = random(0, height);
vPoints[i] = new PVector((int)points[i][0], (int)points[i][1]);
xSpeed[i] = random(15);
ySpeed[i] = random(5);
}
myVoronoi = new Voronoi( points );
myDelaunay = new Delaunay( points );
myEdges = myVoronoi.getEdges();
dEdges = myDelaunay.getEdges();
myLinks = myDelaunay.getLinks();
}
void draw() {
background(255);
Hull myHull = new Hull( points );
MPolygon myRegion = myHull.getRegion();
fill(255, 200, 200);
noFill();
strokeWeight(10);
myRegion.draw(this);
strokeWeight(2);
for (int i=0; i<myEdges.length; i++)
{
stroke(0);
float startX = myEdges[i][0];
float startY = myEdges[i][1];
float endX = myEdges[i][2];
float endY = myEdges[i][3];
/*
if ((mousePressed) && dist(mouseX, mouseY, startX, startY) < 20){
myEdges[i][0] = mouseX;
myEdges[i][1] = mouseY;
ellipse(mouseX, mouseY, 100, 100);
} else if ((mousePressed) && dist(mouseX, mouseY, endX, endY) < 20){
myEdges[i][2] = mouseX;
myEdges[i][3] = mouseY;
ellipse(mouseX, mouseY, 100, 100);
}
*/
line( startX, startY, endX, endY );
stroke(200, 50, 50);
int startIndex = myLinks[i][0];
int endIndex = myLinks[i][1];
startX = points[startIndex][0];
startY = points[startIndex][1];
endX = points[endIndex][0];
endY = points[endIndex][1];
line( startX, startY, endX, endY );
if ((mousePressed) && dist(mouseX, mouseY, startX, startY) < 20) {
points[startIndex][0] = mouseX;
points[startIndex][1] = mouseY;
ellipse(mouseX, mouseY, 100, 100);
reset();
} else if ((mousePressed) && dist(mouseX, mouseY, endX, endY) < 20) {
points[endIndex][0] = mouseX;
points[endIndex][1] = mouseY;
ellipse(mouseX, mouseY, 100, 100);
reset();
}
noFill();
//ellipse(startX, startY, 100, 100);
}
//for(int i = 0; i < vPoints.length; i++){
// ellipse(vPoints[i].x, vPoints[i].y, 10, 10);
//}
if (mousePressed) {
}
if (keyPressed) {
//setup();
for (int i = 0; i < numPoints; i++) {
points[i][0] += sin(millis()) * xSpeed[i];
points[i][1] += sin(millis()) * ySpeed[i];
}
reset();
}
}
void mouseReleased() {
println("reset");
}
void reset(){
myVoronoi = new Voronoi( points );
myDelaunay = new Delaunay( points );
myEdges = myVoronoi.getEdges();
dEdges = myDelaunay.getEdges();
myLinks = myDelaunay.getLinks();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment