Previously we built a basic subdivision system that linearly split image regions using a dynamic array. While it worked visually, it treated all quads equally (regardless of how much visual detail they contained).
In this final part we fix the biggest issue: treating all quads equally.
We introduce a priority queue using a minimum heap that scores each quad based on color error and area. This allows us to render the image more intelligently, refining the most visually important regions first.
What You’ll Learn
- How to implement a min-heap using
stb_ds
dynamic arrays - How to define a score based on color error and region area
- How to use the heap to always split the most important quad next
- How this small change results in better visual quality
- How to limit window size for large images while keeping aspect ratio
What It Does
- Each quad gets a score:
score = -error × (area ** 0.25) + (is_leaf ? 1000000 : 0)
- Quads are stored in a min-heap based on this score
- On each key press, the highest priority quad is split
- The image now refines in a structured and intentional way
Why It Works Better
- Large and high-error regions get split first
- Flat or tiny regions are ignored or delayed
- Rendering looks smarter and converges faster
- Visually the image appears to “come into focus”
Project Code
You will find the complete source code here: quadtree-art
Final Thoughts
What began as a simple recursive drawing routine is now a guided and error aware rendering system. This last step feels like the quadtree is actively seeing: prioritizing what deserves detail.
A small data structure change, a big leap in clarity.
External Resources
Acknowledgements
- Quads by Michael Fogleman - original idea