main
Raw Download raw file
 1package main
 2
 3import (
 4	"fmt"
 5	"math"
 6	"math/rand/v2"
 7
 8	"gonum.org/v1/gonum/spatial/kdtree"
 9)
10
11type Location struct {
12	a uint16
13	e uint16
14	m uint16
15	v uint16
16}
17
18func NewLocation() Location {
19	return Location{
20		a: uint16(rand.UintN(1 << 16)),
21		e: uint16(rand.UintN(1 << 16)),
22		m: uint16(rand.UintN(1 << 16)),
23		v: uint16(rand.UintN(1 << 16)),
24	}
25}
26
27func (l Location) String() string {
28	return fmt.Sprintf("%04X:%04X:%04X:%04X",
29		l.a, l.e, l.m, l.v)
30}
31
32// Compare returns the signed distance of a from the plane passing through
33// b and perpendicular to the dimension d.
34//
35// Given c = a.Compare(b, d):
36//
37//	c = a_d - b_d
38func (l Location) Compare(c kdtree.Comparable, d kdtree.Dim) float64 {
39	q := c.(Location)
40	switch d {
41	case 0: // a
42		return float64(l.a - q.a)
43	case 1: // e
44		return float64(l.e - q.e)
45	case 2: // m
46		return float64(l.m - q.m)
47	case 3: // v
48		return float64(l.v - q.v)
49	default:
50		panic("illegal dimension")
51	}
52}
53
54// Dims returns the number of dimensions described in the Comparable.
55func (l Location) Dims() int {
56	return 4
57}
58
59// Distance returns the squared Euclidean distance between the receiver and
60// the parameter.
61func (l Location) Distance(c kdtree.Comparable) float64 {
62	q := c.(Location)
63	da := l.a - q.a
64	de := l.e - q.e
65	dm := l.m - q.m
66	dv := l.v - q.v
67
68	return math.Sqrt(float64(da*da + de*de + dm*dm + dv*dv))
69}