Practicing graphical debugging using visualizations of the Hilbert curve

https://news.ycombinator.com/rss Hits: 5
Summary

Jan 31, 2025 Practicing graphical debugging using too many visualizations of the Hilbert curve “..you don't understand things, you just get used to them.” — John von Neumann For a while now I've been advocating for a particular style of programming: Use tools that don't change too often. Use tools that don't keep historical accidents around indefinitely. Minimize moving parts. Avoid additional third-party libraries, and forswear native libraries entirely. Lua and LÖVE have been one nice way to get these properties. As I've used them, I've enjoyed an additional benefit: the ubiquitous presence of a canvas I can draw on as I program. This has been new to me with my erstwhile conservative and terminal-bound habits, and I've been pushing myself to lean more on graphics to understand what my programs are doing. Here I want to share one such experience. I'm using my run-anywhere Lua Carousel app, and you can paste the programs directly into it, but the workflow translates to any platform with a canvas. A few weeks ago Jack Rusher shared a baffling function to compute the Hilbert curve. Here it is, translated to Lua: function h(x, y, xi, yi, xj, yj, n) if n <= 0 then return {x+xi/2+xj/2, y+yi/2+yj/2} end return array_join( h(x, y, xj/2, yj/2, xi/2, yi/2, n-1), h(x+xi/2, y+yi/2, xi/2, yi/2, xj/2, yj/2, n-1), h(x+xi/2+xj/2, y+yi/2+yj/2, xi/2, yi/2, xj/2, yj/2, n-1), h(x+xi/2+xj, y+yi/2+yj, -xj/2, -yj/2, -xi/2, -yi/2, n-1)) end When I first looked at it, I could see a few superficial facts: It returns an array of points containing x and y coordinates. It's recursive. There's a base case for “leaf” calls, and each non-base case makes 4 recursive calls. Only the base case actually “draws” by adding points to the result. Non-leaf calls recursively partition the given square into 4 quadrants. Square size (the xi/yi/xj/yj) is being halved each time. But the details were still unclear. Why the swaps/rotations? Why the negative signs in one of the 4 quadrants? Looking for answers le...

First seen: 2025-05-22 19:26

Last seen: 2025-05-22 23:27