Skip to content

Commit

Permalink
add clear method
Browse files Browse the repository at this point in the history
  • Loading branch information
elbywan committed Aug 20, 2017
1 parent c6ec241 commit bff7a53
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 3 deletions.
12 changes: 12 additions & 0 deletions build/js/quadtree.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/js/quadtree.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/js/quadtree.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/js/quadtree.min.js.map

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/quadtree.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@

# ### Exposed methods

# Removes all elements from the quadtree and restores pristine state.
clear: ->
@contents = []
@oversized = []
@size = 0
for child of @children
@children[child].tree = null

# Add an element to the quadtree.
# Elements can be observed to reorganize them into the quadtree automatically whenever their coordinates or dimensions are set (for ex. obj.x = ...).
push: (item, doObserve) ->
Expand Down
14 changes: 14 additions & 0 deletions test/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ describe 'quadtree', ->
assert.equal quadtree.remove(x: 10, y: 10), false
assert.equal quadtree.size, 1

it 'should clean itself properly', ->
quadtree = new Quadtree width: 100, height: 100
quadtree.pushAll [
element0 = x: 75, y: 80, width: 10, height: 10,
element1 = x: 80, y: 85, width: 15, height: 10,
element2 = x: 10, y: 15, width: 1, height: 1,
element3 = x: 12, y: 19, width: 1, height: 1 ]
assert.equal quadtree.size, 4
assert.equal(quadtree.children.NW.tree.children.NW.tree.children.SE.tree.contents[0], element3)
quadtree.clear()
quadtree.pushAll [ element0, element1, element2, element3 ]
assert.equal quadtree.size, 4
assert.equal(quadtree.children.NW.tree.children.NW.tree.children.SE.tree.contents[0], element3)

it 'should detect colliding elements', ->
quadtree = new Quadtree width: 100, height: 100
quadtree.pushAll [
Expand Down
5 changes: 5 additions & 0 deletions typings/quadtree.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ declare class Quadtree<T extends Quadtree.QuadtreeItem> {
maxElements?: number
})

/**
* Removes all elements from the quadtree and restores pristine state.
*/
public clear() : void

/**
* Add an element to the quadtree.
*
Expand Down

0 comments on commit bff7a53

Please sign in to comment.