github.com/pzsz/voronoi@v0.0.0-20130609164533-4314be88c79f/voronoi_test.go (about) 1 // MIT License: See https://github.com/pzsz/voronoi/LICENSE.md 2 3 // Author: Przemyslaw Szczepaniak (przeszczep@gmail.com) 4 // Port of Raymond Hill's (rhill@raymondhill.net) javascript implementation 5 // of Steven Forune's algorithm to compute Voronoi diagrams 6 7 package voronoi_test 8 9 import ( 10 . "github.com/pzsz/voronoi" 11 "math/rand" 12 "testing" 13 ) 14 15 func verifyDiagram(diagram *Diagram, edgesCount, cellsCount, perCellCount int, t *testing.T) { 16 if len(diagram.Edges) != edgesCount { 17 t.Errorf("Expected %d edges not %d", edgesCount, len(diagram.Edges)) 18 } 19 20 if len(diagram.Cells) != cellsCount { 21 t.Errorf("Expected %d cells not %d", cellsCount, len(diagram.Cells)) 22 } 23 24 if perCellCount > 0 { 25 for _, cell := range diagram.Cells { 26 if len(cell.Halfedges) != perCellCount { 27 t.Errorf("Expected per cell edge count expected %d, not %d", perCellCount, len(cell.Halfedges)) 28 } 29 } 30 } 31 } 32 33 func TestVoronoi2Points(t *testing.T) { 34 sites := []Vertex{ 35 Vertex{4, 5}, 36 Vertex{6, 5}, 37 } 38 39 verifyDiagram(ComputeDiagram(sites, NewBBox(0, 10, 0, 10), true), 40 7, 2, 4, t) 41 verifyDiagram(ComputeDiagram(sites, NewBBox(0, 10, 0, 10), false), 42 1, 2, 1, t) 43 } 44 45 func TestVoronoi3Points(t *testing.T) { 46 sites := []Vertex{ 47 Vertex{4, 5}, 48 Vertex{6, 5}, 49 Vertex{5, 8}, 50 } 51 52 verifyDiagram(ComputeDiagram(sites, NewBBox(0, 10, 0, 10), true), 53 10, 3, -1, t) 54 verifyDiagram(ComputeDiagram(sites, NewBBox(0, 10, 0, 10), false), 55 3, 3, 2, t) 56 } 57 58 func Benchmark1000(b *testing.B) { 59 rand.Seed(1234567) 60 b.StopTimer() 61 sites := make([]Vertex, 100) 62 for j := 0; j < 100; j++ { 63 sites[j].X = rand.Float64() * 100 64 sites[j].Y = rand.Float64() * 100 65 } 66 b.StartTimer() 67 ComputeDiagram(sites, NewBBox(0, 100, 0, 100), true) 68 }