Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Quadtree.remove #227

Merged
merged 1 commit into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions orx-quadtree/src/commonMain/kotlin/IQuadtree.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ interface IQuadtree<T> {
*/
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)
*
Expand Down
11 changes: 11 additions & 0 deletions orx-quadtree/src/commonMain/kotlin/Quadtree.kt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ class Quadtree<T>(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)
*
Expand Down
6 changes: 6 additions & 0 deletions orx-quadtree/src/jvmMain/kotlin/ReadwriteQuadtree.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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>? {
lock.read {
return qt.findNode(element)
Expand Down