-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqnode.go
152 lines (139 loc) · 3.76 KB
/
qnode.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"fmt"
"strings"
)
type Locatable interface {
getPosition() (x, y float32)
}
type QNode[T Locatable] struct {
northwest, northeast, southwest, southeast *QNode[T]
datapoints []T
marked bool
x0, x1, y0, y1 float32
depth uint
}
func NewQNode[T Locatable](datapoints []T, x0, x1, y0, y1 float32, depth uint) *QNode[T] {
return &QNode[T]{nil, nil, nil, nil, datapoints, false, x0, x1, y0, y1, depth}
}
func (node *QNode[T]) makeNorthWest(datapoints []T) *QNode[T] {
midX, midY := node.getMidValues()
node.northwest = NewQNode(datapoints, node.x0, midX, node.y0, midY, node.depth+1)
return node.northwest
}
func (node *QNode[T]) makeNorthEast(datapoints []T) *QNode[T] {
midX, midY := node.getMidValues()
node.northeast = NewQNode(datapoints, midX, node.x1, node.y0, midY, node.depth+1)
return node.northeast
}
func (node *QNode[T]) makeSouthWest(datapoints []T) *QNode[T] {
midX, midY := node.getMidValues()
node.southwest = NewQNode(datapoints, node.x0, midX, midY, node.y1, node.depth+1)
return node.southwest
}
func (node *QNode[T]) makeSouthEast(datapoints []T) *QNode[T] {
midX, midY := node.getMidValues()
node.southeast = NewQNode(datapoints, midX, node.x1, midY, node.y1, node.depth+1)
return node.southeast
}
func (node *QNode[T]) forEach(skip func(node *QNode[T]) bool, maxDepth uint) {
if node.depth > maxDepth {
return
}
if skip(node) {
return
}
if node.northwest != nil {
node.northwest.forEach(skip, maxDepth)
}
if node.northeast != nil {
node.northeast.forEach(skip, maxDepth)
}
if node.southwest != nil {
node.southwest.forEach(skip, maxDepth)
}
if node.southeast != nil {
node.southeast.forEach(skip, maxDepth)
}
}
func (node *QNode[T]) markPathTo(x, y float32) {
node.marked = true
midX, midY := node.getMidValues()
switch {
case x < midX && y < midY:
if node.northwest == nil {
return
}
node.northwest.markPathTo(x, y)
case x > midX && y < midY:
if node.northeast == nil {
return
}
node.northeast.markPathTo(x, y)
case x < midX && y > midY:
if node.southwest == nil {
return
}
node.southwest.markPathTo(x, y)
case x > midX && y > midY:
if node.southeast == nil {
return
}
node.southeast.markPathTo(x, y)
}
}
func (node *QNode[T]) generateTree(maxDepth uint) {
node.forEach(func(node *QNode[T]) bool {
nwDatapoints := make([]T, 0)
neDatapoints := make([]T, 0)
swDatapoints := make([]T, 0)
seDatapoints := make([]T, 0)
for _, v := range node.datapoints {
x, y := v.getPosition()
midX, midY := node.getMidValues()
switch {
case x < midX && y < midY:
nwDatapoints = append(nwDatapoints, v)
case x > midX && y < midY:
neDatapoints = append(neDatapoints, v)
case x < midX && y > midY:
swDatapoints = append(swDatapoints, v)
case x > midX && y > midY:
seDatapoints = append(seDatapoints, v)
}
}
noData := true
if len(nwDatapoints) != 0 {
noData = false
node.makeNorthWest(nwDatapoints)
}
if len(neDatapoints) != 0 {
noData = false
node.makeNorthEast(neDatapoints)
}
if len(swDatapoints) != 0 {
noData = false
node.makeSouthWest(swDatapoints)
}
if len(seDatapoints) != 0 {
noData = false
node.makeSouthEast(seDatapoints)
}
if noData {
return true
}
return false
}, maxDepth)
}
func (node *QNode[T]) getMidValues() (x, y float32) {
return (node.x0 + node.x1) * 0.5, (node.y0 + node.y1) * 0.5
}
func (node *QNode[T]) String() string {
var sb strings.Builder
node.forEach(func(node *QNode[T]) bool {
midX, midY := node.getMidValues()
sb.WriteString(fmt.Sprintf("%s[Node: (%f, %f)]\n", strings.Repeat("-> ", int(node.depth)), midX, midY))
return false
}, 10)
return sb.String()
}