File: kdtree_simple_example_test.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (77 lines) | stat: -rw-r--r-- 2,291 bytes parent folder | download
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
69
70
71
72
73
74
75
76
77
// Copyright ©2019 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package kdtree_test

import (
	"fmt"
	"math"

	"gonum.org/v1/gonum/spatial/kdtree"
)

func ExampleTree() {
	// Example data from https://en.wikipedia.org/wiki/K-d_tree
	points := kdtree.Points{{2, 3}, {5, 4}, {9, 6}, {4, 7}, {8, 1}, {7, 2}}

	t := kdtree.New(points, false)
	q := kdtree.Point{8, 7}
	p, d := t.Nearest(q)
	fmt.Printf("%v is closest point to %v, d=%f\n", p, q, math.Sqrt(d))
	// Output:
	// [9 6] is closest point to [8 7], d=1.414214
}

func ExampleTree_bounds() {
	// Example data from https://en.wikipedia.org/wiki/K-d_tree
	points := kdtree.Points{{2, 3}, {5, 4}, {9, 6}, {4, 7}, {8, 1}, {7, 2}}

	t := kdtree.New(points, true)
	fmt.Printf("Bounding box of points is %+v\n", t.Root.Bounding)
	// Output:
	// Bounding box of points is &{Min:[2 1] Max:[9 7]}
}

func ExampleTree_Do() {
	// Example data from https://en.wikipedia.org/wiki/K-d_tree
	points := kdtree.Points{{2, 3}, {5, 4}, {9, 6}, {4, 7}, {8, 1}, {7, 2}}

	// Print all points in the data set within 3 of (3, 5).
	t := kdtree.New(points, false)
	q := kdtree.Point{3, 5}
	t.Do(func(c kdtree.Comparable, _ *kdtree.Bounding, _ int) (done bool) {
		// Compare each distance and output points
		// with a Euclidean distance less than 3.
		// Distance returns the square of the
		// Euclidean distance between points.
		if q.Distance(c) <= 3*3 {
			fmt.Println(c)
		}
		return
	})
	// Unordered output:
	// [2 3]
	// [4 7]
	// [5 4]
}

func ExampleTree_DoBounded() {
	// Example data from https://en.wikipedia.org/wiki/K-d_tree
	points := kdtree.Points{{2, 3}, {5, 4}, {9, 6}, {4, 7}, {8, 1}, {7, 2}}

	// Find all points within the bounding box ((3, 3), (6, 8))
	// and print them with their bounding boxes and tree depth.
	t := kdtree.New(points, true) // Construct tree with bounding boxes.
	b := &kdtree.Bounding{
		Min: kdtree.Point{3, 3},
		Max: kdtree.Point{6, 8},
	}
	t.DoBounded(b, func(c kdtree.Comparable, bound *kdtree.Bounding, depth int) (done bool) {
		fmt.Printf("p=%v bound=%+v depth=%d\n", c, bound, depth)
		return
	})
	// Output:
	// p=[5 4] bound=&{Min:[2 3] Max:[5 7]} depth=1
	// p=[4 7] bound=&{Min:[4 7] Max:[4 7]} depth=2
}