Hemesh : voronoi example

voronoi example


//requires hemesh 1.2.3
import wblut.hemesh.*;
import wblut.hemesh.modifiers.*;
import wblut.hemesh.creators.*;
import wblut.geom.*;

float[][] points;
int numpoints;
HE_Mesh container;
HE_Mesh[] cells;
int numcells;
WB_Plane P1,P2;

void setup() {
  size(800,800,P3D);
  //create a sphere
  HEC_Geodesic geo=new HEC_Geodesic(this);
  geo.setRadius(300).setLevel(2); 
  container=new HE_Mesh(geo);
  
  //slice off most of both hemispheres
  P1=new WB_Plane(new WB_Point(0,0,-10), new WB_Vector(0,0,1));
  P2=new WB_Plane(new WB_Point(0,0,10), new WB_Vector(0,0,-1));
  HEM_Slice s=new HEM_Slice().setPlane(P1);
  container.modify(s);
  s=new HEM_Slice().setPlane(P2);
  container.modify(s);
  
  //generate points
  numpoints=50;
  points=new float[numpoints][3];
  for(int i=0;i<numpoints;i++) {
    points[i][0]=random(-250,250);
    points[i][1]=random(-250,250);
    points[i][2]=random(-20,20);
  }
  
  //generate voronoi cells
  HEMC_VoronoiCells vcmc=new HEMC_VoronoiCells(this);
  vcmc.setPoints(points).setContainer(container).setOffset(5);
  cells=vcmc.create();
  numcells=cells.length;
}

void draw() {
  background(255);
  lights();
  translate(width/2,height/2,0);
  rotateX(1f/height*mouseY*TWO_PI-PI);
  rotateY(1f/width*mouseX*TWO_PI-PI);
  drawFaces();
  drawEdges();
}

void drawEdges(){
  smooth();
  stroke(0);
  strokeWeight(2);
  for(int i=0;i<numcells;i++) {
    cells[i].drawEdges();
  } 
}

void drawFaces(){
  noSmooth();
  noStroke();
  for(int i=0;i<numcells;i++) {
    fill(100+i,i,i);
    cells[i].drawFaces();
  }   
}

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>