@@ -16,7 +16,7 @@ data class QuadtreeQuery<T>(val nearest: T, val neighbours: List<T>, val quads:
16
16
* @property maxObjects maximum number of objects per node
17
17
* @property mapper
18
18
*/
19
- class Quadtree <T >(val bounds : Rectangle , val maxObjects : Int = 10 , val mapper : ((T ) -> Vector2 )) {
19
+ class Quadtree <T >(val bounds : Rectangle , val maxObjects : Int = 10 , val mapper : ((T ) -> Vector2 )) : IQuadtree<T> {
20
20
/* *
21
21
* The 4 nodes of the tree
22
22
*/
@@ -30,7 +30,7 @@ class Quadtree<T>(val bounds: Rectangle, val maxObjects: Int = 10, val mapper: (
30
30
/* *
31
31
* Clears the whole tree
32
32
*/
33
- fun clear () {
33
+ override fun clear () {
34
34
objects.clear()
35
35
36
36
for (i in nodes.indices) {
@@ -42,14 +42,56 @@ class Quadtree<T>(val bounds: Rectangle, val maxObjects: Int = 10, val mapper: (
42
42
}
43
43
}
44
44
45
+ /* *
46
+ * Finds the nearest and neighbouring objects within a radius
47
+ * (needs to have a different name so there is no ambiguity when the generic object type is Vector2)
48
+ *
49
+ * @param point
50
+ * @param radius
51
+ * @return
52
+ */
53
+ override fun nearestToPoint (point : Vector2 , radius : Double ): QuadtreeQuery <T >? {
54
+ if (! bounds.contains(point)) return null
55
+
56
+ val r2 = radius * radius
57
+
58
+ val scaledBounds = Rectangle .fromCenter(point, radius * 2 )
59
+ val intersected: List <Quadtree <T >> = intersect(scaledBounds) ? : return null
60
+
61
+ var minDist = Double .MAX_VALUE
62
+ val nearestObjects = mutableListOf<T >()
63
+ var nearestObject: T ? = null
64
+
65
+ for (interNode in intersected) {
66
+ for (obj in interNode.objects) {
67
+ val p = mapper(obj)
68
+
69
+ val dist = p.squaredDistanceTo(point)
70
+
71
+ if (dist < r2) {
72
+ nearestObjects.add(obj)
73
+
74
+ if (dist < minDist) {
75
+ minDist = dist
76
+ nearestObject = obj
77
+ }
78
+ }
79
+ }
80
+ }
81
+
82
+ if (nearestObject == null ) return null
83
+
84
+ return QuadtreeQuery (nearestObject, nearestObjects, intersected)
85
+ }
86
+
45
87
/* *
46
88
* Finds the nearest and neighbouring points within a radius
47
89
*
48
90
* @param element
49
91
* @param radius
50
92
* @return
51
93
*/
52
- fun nearest (element : T , radius : Double ): QuadtreeQuery <T >? {
94
+ override fun nearest (element : T , radius : Double ): QuadtreeQuery <T >? {
53
95
val point = mapper(element)
54
96
55
97
if (! bounds.contains(point)) return null
@@ -92,7 +134,7 @@ class Quadtree<T>(val bounds: Rectangle, val maxObjects: Int = 10, val mapper: (
92
134
* @param element
93
135
* @return
94
136
*/
95
- fun insert (element : T ): Boolean {
137
+ override fun insert (element : T ): Boolean {
96
138
// only* the root needs to check this
97
139
if (depth == 0 ) {
98
140
if (! bounds.contains(mapper(element))) return false
@@ -128,7 +170,7 @@ class Quadtree<T>(val bounds: Rectangle, val maxObjects: Int = 10, val mapper: (
128
170
* @param element
129
171
* @return
130
172
*/
131
- fun findNode (element : T ): Quadtree <T >? {
173
+ override fun findNode (element : T ): Quadtree <T >? {
132
174
val v = mapper(element)
133
175
134
176
if (! bounds.contains(v)) return null
0 commit comments