Voronoi diagrams using 3D cones

versions102+
contributorstomc
started on2006-03-10 17:40

voronoi_cone.jar

This is a first pass attempt at converting a classic OpenGL hardware hack into Processing 3D.

FIXME Voronoi background, links etc. Links to the OpenGL Red Book original.

Source code

This code also requires the cone code to run.

FIXME my use of ortho in Processing 102 with P3D doesn't seem quite right… this needs investigating.

/**
voronoicones taken from http://processinghacks.com/hacks:voronoicones
@author Tom Carden
*/
 
// draws 32 points and their approximate voronoi regions
// only using cones, and P3D's depth buffer
 
int NUM_POINTS = 12;
float x[];  // point x
float y[];  // point y
float av[]; // point angular velocity
color c[];  // point color
 
void setup() {
  size(400,400,P3D);
 
  // FIXME: ortho seems to be a bit strange, I am investigating
  ortho(-width/2,width/2,-height/2,height/2,-10,10);
 
  x = new float[NUM_POINTS];
  y = new float[NUM_POINTS];
  av = new float[NUM_POINTS];
  c = new color[NUM_POINTS];
 
  for (int i = 0; i < NUM_POINTS; i++) {
    x[i] = random(width);
    y[i] = random(height);
    av[i] = random(-0.02,0.02);
    c[i] = color(random(255),random(255),random(255));
  }
 
}
 
void draw() {
 
  background(0);
 
  // draw the cones
  pushMatrix();
  // -10 means the cones are below the surface
  // (so we can draw on top of them later)
  translate(0,0,-10);
  for (int i = 0; i < NUM_POINTS; i++) {
    fill(c[i]);
    noStroke();
    // if you have more points, use a smaller radius 
    // (but you risk having gaps)
    cone(x[i],y[i],200.0,10.0); 
  }
  popMatrix();
 
  // give user control of first point
  x[0] = mouseX;
  y[0] = mouseY;
 
  // on mouse pressed, orbit the points around the centre of the screen
  if (mousePressed) {  
    for (int i = 1; i < NUM_POINTS; i++) {
      float r = dist(width/2,height/2,x[i],y[i]);
      float a = atan2(y[i]-(height/2),x[i]-(width/2)) + av[i];
      x[i] = (width/2) + (r * cos(a));
      y[i] = (height/2) + (r * sin(a));
    }
  }
 
  // draw dots for the points
  for (int i = 0; i < NUM_POINTS; i++) {
    fill(0);
    noStroke();
    ellipse(x[i],y[i],5.0,5.0);
  }
 
}

Downloads

Related Links

Please list any links you think are essential reading related to this hack:

http://processing.org

hacks/voronoicones.txt · Last modified: 2007-07-13 02:12 (external edit)