diff --git a/orx-quadtree/src/commonMain/kotlin/IQuadtree.kt b/orx-quadtree/src/commonMain/kotlin/IQuadtree.kt index f7e2a94a9..b0ae539a2 100644 --- a/orx-quadtree/src/commonMain/kotlin/IQuadtree.kt +++ b/orx-quadtree/src/commonMain/kotlin/IQuadtree.kt @@ -35,6 +35,14 @@ interface IQuadtree { */ 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) * diff --git a/orx-quadtree/src/commonMain/kotlin/Quadtree.kt b/orx-quadtree/src/commonMain/kotlin/Quadtree.kt index 8b63227b6..c211a2a37 100644 --- a/orx-quadtree/src/commonMain/kotlin/Quadtree.kt +++ b/orx-quadtree/src/commonMain/kotlin/Quadtree.kt @@ -164,6 +164,17 @@ class Quadtree(val bounds: Rectangle, val maxObjects: Int = 10, val mapper: ( 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) * diff --git a/orx-quadtree/src/jvmMain/kotlin/ReadwriteQuadtree.kt b/orx-quadtree/src/jvmMain/kotlin/ReadwriteQuadtree.kt index 5417d3b5c..47d0c3884 100644 --- a/orx-quadtree/src/jvmMain/kotlin/ReadwriteQuadtree.kt +++ b/orx-quadtree/src/jvmMain/kotlin/ReadwriteQuadtree.kt @@ -36,6 +36,12 @@ class ReadwriteQuadtree(val qt: Quadtree) : IQuadtree { } } + override fun remove(element: T): Boolean { + lock.write { + return qt.remove(element) + } + } + override fun findNode(element: T): Quadtree? { lock.read { return qt.findNode(element)