logoalt Hacker News

Functional Quadtrees

91 pointsby lbjtoday at 1:18 PM33 commentsview on HN

Comments

lemonwaterlimetoday at 3:54 PM

I like to do data-oriented programming, and was just thinking about how I want to organize (and search through) the primary data structures/concepts for a project I'm working on. Part of that involved thinking about things like what information I might cache and what representations data might take. That lead me to looking into the nuances of things like B-Trees, AVL Trees, Quadtrees, k-d trees and so forth.

I've found the book "Foundations of Multidimensional and Metric Data Structures" by Hanan Samet to be an excellent resource when looking for a slightly deeper dive than a more introductory algorithms course. It goes in depth on the nuances of these approaches, many of which are highly similar at a cursory glance.

show 1 reply
marvinbornertoday at 3:11 PM

Quadtrees are also quite useful for generating fractals. A very related project of mine, Lambda Screen [0], explores this by encoding these functional quadtrees directly in lambda calculus and rendering the structure based on Church booleans being true (white) or false (black).

With fixed point recursion, this allows for very tiny definitions of IFS fractals. For example, fractals like the Sierpinski triangle/carpet only require ~50 bit of binary lambda calculus [1] [2]!

[0]: https://text.marvinborner.de/2024-03-25-02.html

[1]: https://lambda-screen.marvinborner.de/?term=ERoc0CrYLYA%3D

[2]: https://lambda-screen.marvinborner.de/?term=QcCqqttsFtsI0OaA

torusletoday at 2:24 PM

"I could only find a couple tutorials/guides and both were imperative"

Aren't Quadtrees covered by almost all basic data-structure books? It is the most simple form of taking the binary tree into the next (2D) dimension.

show 2 replies
OisinMorantoday at 2:02 PM

Neat! Weirdly sending this article from my phone (Pixel 8) to my browser (Arc) via Pushbullet resulted in an incredibly strange bug that it loads this site instead:

https://www.lindelystables.dk/en/posts/functional-quadtree-c...

Got very confused! I challenge the HN hivemind to figure out what's going on.

show 2 replies
Waterluviantoday at 1:54 PM

I love the visualization, which gave me an idea: what if we numbered every "Looking at" step in the visualization? Then it's obvious just how many search steps it takes.

And then maybe even juxtapose that with a linear search example, which also numbers every step. I bet this would make it really click for some people. And for free the user can also play with how a linear search can sometimes be faster when they just want the first element!

As a bonus: allow the user to change the cell count so they can really feel just how each method scales!

runemadsentoday at 2:10 PM

We just did a whole visual identity around the quadtree concept. Take a scroll on this one! https://trace.systems/

show 3 replies
willvarfartoday at 2:39 PM

A general quadtree implementation question that puzzled me when I was implementing it myself for hobby games was: do you store a rectangle in the smallest node that completely contains it?

Most code that I saw that used quadtrees were treating things as points and storing them only at the lowest level.

I also made mine auto-divide by counting items that are entirely in a quadrant as they are added to the node, with allocate and split triggered if a count went above a certain threshold.

Anything novel or oopsie?

show 3 replies
wiz21ctoday at 2:11 PM

I think it is weird to have two cells divided downto their smallest size when my cursor clearly occupies only one of them, not two.

enigma101today at 6:33 PM

The language hurts the eyes

senderistatoday at 3:43 PM

I wish the article had made it clearer that quadtree positions are encoded as strings over the alphabet on 2 bits (similarly, octrees use the alphabet over 3 bits). This makes storing keys and lexicographically comparing them very simple.

show 1 reply
TacticalCodertoday at 3:48 PM

[dead]