gonum.org/v1/gonum@v0.14.0/spatial/vptree/vptree_simple_example_test.go (about) 1 // Copyright ©2019 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package vptree_test 6 7 import ( 8 "fmt" 9 "log" 10 11 "gonum.org/v1/gonum/spatial/vptree" 12 ) 13 14 func ExampleTree() { 15 // Example data from https://en.wikipedia.org/wiki/K-d_tree 16 points := []vptree.Comparable{ 17 vptree.Point{2, 3}, 18 vptree.Point{5, 4}, 19 vptree.Point{9, 6}, 20 vptree.Point{4, 7}, 21 vptree.Point{8, 1}, 22 vptree.Point{7, 2}, 23 } 24 25 t, err := vptree.New(points, 3, nil) 26 if err != nil { 27 log.Fatal(err) 28 } 29 q := vptree.Point{8, 7} 30 p, d := t.Nearest(q) 31 fmt.Printf("%v is closest point to %v, d=%f\n", p, q, d) 32 // Output: 33 // [9 6] is closest point to [8 7], d=1.414214 34 } 35 36 func ExampleTree_Do() { 37 // Example data from https://en.wikipedia.org/wiki/K-d_tree 38 points := []vptree.Comparable{ 39 vptree.Point{2, 3}, 40 vptree.Point{5, 4}, 41 vptree.Point{9, 6}, 42 vptree.Point{4, 7}, 43 vptree.Point{8, 1}, 44 vptree.Point{7, 2}, 45 } 46 47 // Print all points in the data set within 3 of (3, 5). 48 t, err := vptree.New(points, 0, nil) 49 if err != nil { 50 log.Fatal(err) 51 } 52 q := vptree.Point{3, 5} 53 t.Do(func(c vptree.Comparable, _ int) (done bool) { 54 // Compare each distance and output points 55 // with a Euclidean distance less than or 56 // equal to 3. Distance returns the 57 // Euclidean distance between points. 58 if q.Distance(c) <= 3 { 59 fmt.Println(c) 60 } 61 return 62 }) 63 // Unordered output: 64 // [2 3] 65 // [4 7] 66 // [5 4] 67 }