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

Conversation

AndreAzevedoCoder
Copy link

#8
Added Delete, Delete in Range and Move a point(or a rectangle/circle).
It's pretty simple and maybe not optimized.
Hope it helps someone that just like me need it for a game or something like that.

@crhallberg
Copy link
Collaborator

This looks really good! Would it be possible to write a test or two for these changes?

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.

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!

Base automatically changed from master to main March 2, 2021 18:33
@crhallberg
Copy link
Collaborator

Hey! I've gotten upgraded permissions to the repository so I'm trying to do some clean up! Any interest in getting this up to date?

@AndreAzevedoCoder
Copy link
Author

Of course! What can I do?

@crhallberg
Copy link
Collaborator

crhallberg commented Mar 4, 2021

Great! Step 1 is merging with main and fixing conflicts.

After that, I think we left it with two comments that need addressing and missing some tests.

Are there any parts of this you want me to tackle?

@crhallberg
Copy link
Collaborator

I've been thinking about this a lot and I think this approach needs some tweaks. Since the QuadTree isn't providing the IDs, I'm skeptical about relying on IDs for deleting points. Also based on this usage guide for a different implementation of QuadTrees, QuadTrees are more useful to be snapshots and not live systems.

Here's what I propose (and will implement with help from your great code above):

  • deleteFromRange(range): as you've done so.
  • filter(fn): takes a function and returns a new QuadTree built with all true points inserted. This allows for a lot of reuse of what we have and will return a tree with no empty quads or subdivisions.
  • clear(): as requested in Function to clear QuadTree #9.

This was referenced Oct 25, 2021
@crhallberg
Copy link
Collaborator

Incorporated into #62, now merged.

@crhallberg crhallberg closed this Oct 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants