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