Skip to content

Voronoi Diagrams

Allen Ray edited this page Aug 15, 2020 · 1 revision

pzsz provides a fast and easy-to-use golang voronoi calculator that this tool takes advantage of pzsz's Voronoi Diagrams. This tool is simply a compatibility interface into the above project so that it's calculator can be easily used with pixel.

v := pixelutils.NewVoronoi()

id := 0 // you should probably keep track of this a different way
for x := 0; x < 100; x++ {
  for y := 0; y < 100; y++ {
    v.Insert(id, pixel.V(x, y))
    id++
  }
}

// This will compute and bind the diagram to the rectangle
//  created with the min, max points (-100, -100), (100, 100)
cells := v.Compute(pixel.R(-100, -100, 100, 100), true)

Drawing the Diagram

This tools leaves the specifics of the drawing to you as it doesn't want to assume what you want the diagram to look like.

The simplest way of doing this would be to draw to a pixel.imdraw. If you're not changing the diagram (adding/removing points), then you could generate the imdraw once and just draw it every frame.

im := imdraw.New(nil) // don't have to create a new one, could use an existing one for performance

for _, cell := range cells {
  // just for fun, make the cell different colors
  im.Color = color.RGBA{uint8(rand.Int()), uint8(rand.Int()), uint8(rand.Int()), 1}

  for _, edge := range cells.Halfedges {
    im.Push(edge.A, edge.B)
  }
  im.Polygon(0) // 0 for filled in cell
}

// then draw the im later using
im.draw(win)
Clone this wiki locally