add Quadtree.remove (#227)
Co-authored-by: Jonathan Ellis <jbellis@gmail.com>
This commit is contained in:
@@ -35,6 +35,14 @@ interface IQuadtree<T> {
|
|||||||
*/
|
*/
|
||||||
fun insert(element: T): Boolean
|
fun insert(element: T): Boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the given element
|
||||||
|
*
|
||||||
|
* @param element
|
||||||
|
* @return true if the element was present
|
||||||
|
*/
|
||||||
|
fun remove(element: T): Boolean
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds which node the element is within (but not necessarily belonging to)
|
* Finds which node the element is within (but not necessarily belonging to)
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -164,6 +164,17 @@ class Quadtree<T>(val bounds: Rectangle, val maxObjects: Int = 10, val mapper: (
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun remove(element: T): Boolean {
|
||||||
|
if (isLeaf) {
|
||||||
|
return objects.remove(element)
|
||||||
|
}
|
||||||
|
val p = mapper(element)
|
||||||
|
val x = if (p.x > bounds.center.x) 1 else 0
|
||||||
|
val y = if (p.y > bounds.center.y) 1 else 0
|
||||||
|
val nodeIndex = x + y * 2
|
||||||
|
return nodes[nodeIndex]!!.remove(element)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds which node the element is within (but not necessarily belonging to)
|
* Finds which node the element is within (but not necessarily belonging to)
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -36,6 +36,12 @@ class ReadwriteQuadtree<T>(val qt: Quadtree<T>) : IQuadtree<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun remove(element: T): Boolean {
|
||||||
|
lock.write {
|
||||||
|
return qt.remove(element)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun findNode(element: T): Quadtree<T>? {
|
override fun findNode(element: T): Quadtree<T>? {
|
||||||
lock.read {
|
lock.read {
|
||||||
return qt.findNode(element)
|
return qt.findNode(element)
|
||||||
|
|||||||
Reference in New Issue
Block a user