Skip to content

Delete, Delete in range and Move implemented #42

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

Closed
Closed
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
81 changes: 80 additions & 1 deletion quadtree.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ class Point {
}

class Rectangle {
constructor(x, y, w, h) {
constructor(x, y, w, h, data) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.userData = data;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like below you're using the userData of each Point to store an id. Is userData on a Rectangle part of it that I didn't see?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this just needs to be removed because it's not being used in the code that I can see.

}

get left() {
Expand Down Expand Up @@ -276,10 +277,88 @@ class QuadTree {
this.southwest.query(range, found);
this.southeast.query(range, found);
}


return found;
}

delete(range,id) {


if (!range.intersects(this.boundary)) {
return
}


for( var i = 0; i < this.points.length; i++ ){
var p = this.points[i]
if (range.contains(p)) {
if(p.userData.id == id){
this.points.splice(i,1);
}
}
}

if (this.divided) {
this.northwest.delete(range, id);
this.northeast.delete(range, id);
this.southwest.delete(range, id);
this.southeast.delete(range, id);
}
}

movePoint(range,id,x,y) {


if (!range.intersects(this.boundary)) {
return
}


for( var i = 0; i < this.points.length; i++ ){
var p = this.points[i]
if (range.contains(p)) {
if(p.userData.id == id){
this.points[i].x = x;
this.points[i].y = y;
}
}
}

if (this.divided) {
this.northwest.movePoint(range,id,x,y);
this.northeast.movePoint(range,id,x,y);
this.southwest.movePoint(range,id,x,y);
this.southeast.movePoint(range,id,x,y);
}
}

deleteInRange(range) {
//Index of what needs to be deleted
var deleteArray = [];

for( var i = 0; i < this.points.length; i++ ){
var p = this.points[i]
if (range.contains(p)) {
deleteArray.push(i)
}
}

//When deleted all index change , so the delta fix it
var delta = 0
for(var i = 0; i < deleteArray.length; i++){
this.points.splice(i-delta,1);
delta++
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One approach I like for deleting from an array is to loop backwards thru an array to get around the index changes. Just a tip!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a recommendation to simplify this a bit by running backwards thru the array:

 for( var i = this.points.length; i--;){
    var p = this.points[i];
    if (range.contains(p)) {
      this.points.splice(i, 1);
    }
}

With ES6 we can now try to use a filter, which might be cleaner and faster!


if (this.divided) {
this.northwest.deleteInRange(range);
this.northeast.deleteInRange(range);
this.southwest.deleteInRange(range);
this.southeast.deleteInRange(range);
}
}

closest(point, count, maxDistance) {
if (typeof point === "undefined") {
throw TypeError("Method 'closest' needs a point");
Expand Down