Coding Challenge #98.1: Quadtree - Part 1
Why Stack Overflow recommends it
Stack Overflow users link it when someone needs to speed up collision detection or spatial lookups and wants a clear, visual walkthrough of how a quadtree works.
Recommended by 4 people across 4 different months, 2018 to 2022.
What it is
Daniel Shiffman of The Coding Train implements a Quadtree in JavaScript and visualizes it with p5.js. He starts with the N squared collision problem and Big O notation, then builds the QuadTree class with capacity, subdivision, recursive point insertion and mouse-click point adding.
What people said
Recommends enxaneta · 2018-09 · Stack Overflow · on “How to render and select tiles from a huge plane in Canvas?”
I think the Quadtree is the best solution here. Maybe Daniel Shiffman's tutorial [this video] may give you some ideas.
Recommends user1558604 · 2019-12 · Stack Overflow · on “Nested loops or function application”
Is this something you will need to run more than once? If not, an unoptimized program such as this is fine. Just let it run for however long it takes. (Though I do recommend using something like `tqdm` so you know its progress and expected time to complete). If you need to run it more than once, then you will want to optimize it. I don't have any experience with this, but I have watched this video which was pretty relevant to this topic. [this video] It isn't in python, but the principles remain the same.
Recommends Mike 'Pomax' Kamermans · 2020-02 · Stack Overflow · on “Should Quadtrees store points only in the children?”
Not really a question for Stackoverflow (at least not without some more indications that you searched more than just wikipedia), but hey, have some links to Daniel Shiffman explaining and live-implementing Quad Trees over on the Coding Train youtube channel: [this video] [LINK] and [LINK]
Recommends dwb · 2020-05 · Stack Overflow · on “How could I solve this JS animation issue?”
My bad, I didn't spot that your for loops counteract the 'double collisions' and 'colliding with itself'. I was concerned that `i` and `j` could have the same value. But a method you could use is to split the canvas up into an invisible grid, and then only check for ball collisions inside the same grid cell (eg nearby). This reminds me of a fantastic video on Quadtrees [this video] .
Cites KMot · 2022-07 · Stack Overflow · on “C++ : How do I make this particle simulation faster?”
You are checking each particle against all other particles to detect collision. That is O(n^2) and can slow down your code with increasing number of particles. (Drawing, etc. also take time, but they scale linearly O(n) with number of particles.) A solution is to have less collision detection, e.g. only compare the particles that you know are close to each other. You can use a data-structure like quad-tree for this purpose. This is a video (which is not very optimized) showing the concept: [this video] Of course you can search for quadtree and find more resources.