Commit 8b303dd
2024-10-05 05:58:14
Changed files (5)
go.mod
@@ -0,0 +1,7 @@
+module emc2
+
+go 1.22.0
+
+require gonum.org/v1/gonum v0.15.1
+
+require golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
go.sum
@@ -0,0 +1,4 @@
+golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa h1:FRnLl4eNAQl8hwxVVC17teOw8kdjVDVAiFMtgUdTSRQ=
+golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE=
+gonum.org/v1/gonum v0.15.1 h1:FNy7N6OUZVUaWG9pTiD+jlhdQ3lMP+/LcTpJ6+a8sQ0=
+gonum.org/v1/gonum v0.15.1/go.mod h1:eZTZuRFrzu5pcyjN5wJhcIhnUdNijYxX1T2IcrOGY0o=
location.go
@@ -0,0 +1,69 @@
+package main
+
+import (
+ "fmt"
+ "math"
+ "math/rand/v2"
+
+ "gonum.org/v1/gonum/spatial/kdtree"
+)
+
+type Location struct {
+ a uint16
+ e uint16
+ m uint16
+ v uint16
+}
+
+func NewLocation() *Location {
+ return &Location{
+ a: uint16(rand.UintN(1 << 16)),
+ e: uint16(rand.UintN(1 << 16)),
+ m: uint16(rand.UintN(1 << 16)),
+ v: uint16(rand.UintN(1 << 16)),
+ }
+}
+
+func (l *Location) String() string {
+ return fmt.Sprintf("%04X:%04X:%04X:%04X",
+ l.a, l.e, l.m, l.v)
+}
+
+// Compare returns the signed distance of a from the plane passing through
+// b and perpendicular to the dimension d.
+//
+// Given c = a.Compare(b, d):
+//
+// c = a_d - b_d
+func (l Location) Compare(c kdtree.Comparable, d kdtree.Dim) float64 {
+ q := c.(Location)
+ switch d {
+ case 0: // a
+ return float64(l.a - q.a)
+ case 1: // e
+ return float64(l.e - q.e)
+ case 2: // m
+ return float64(l.m - q.m)
+ case 3: // v
+ return float64(l.v - q.v)
+ default:
+ panic("illegal dimension")
+ }
+}
+
+// Dims returns the number of dimensions described in the Comparable.
+func (l Location) Dims() int {
+ return 4
+}
+
+// Distance returns the squared Euclidean distance between the receiver and
+// the parameter.
+func (l Location) Distance(c kdtree.Comparable) float64 {
+ q := c.(Location)
+ da := l.a - q.a
+ de := l.e - q.e
+ dm := l.m - q.m
+ dv := l.v - q.v
+
+ return math.Sqrt(float64(da*da + de*de + dm*dm + dv*dv))
+}
main.go
@@ -0,0 +1,13 @@
+package main
+
+import (
+ "gonum.org/v1/gonum/spatial/kdtree"
+)
+
+func main() {
+ var galaxy []*Location = make([]*Location, 100)
+ for _ = range 100 {
+ galaxy = append(galaxy, NewLocation())
+ }
+ t := kdtree.New(galaxy, false)
+}
README.md
@@ -0,0 +1,1 @@
+# emc2