Circle Packing
You'll fill a canvas with circles that never overlap, make it fast enough to run in seconds, and pack them inside a word or a photo.
About
Circle packing is the first algorithm where the obvious version is correct and also too slow. You pick a random point, grow a circle there until it touches a neighbor, and reject any point that lands inside an existing circle, which means checking every circle against every other. At a few thousand circles the browser tab starts to chug, and a simple grid of buckets fixes it, which is the lesson. Packing inside a mask, a word or the bright parts of a photo is where it turns into a piece.
At a glance
- Per requirement
- 25 XP
- Completion bonus
- 150 XP
- Requirements
- 5
- Takes
- One weekend
- Skill level
- Intermediate (months of practice)
- Cost
- Free
- Supplies
- Nothing special
- Where
- At home
- Other people
- On your own
Requirements
-
1
Pack a canvas with circles that don't overlap, the slow way. Try a random point, reject it if it lands inside any circle, otherwise grow the radius one pixel at a time until it touches a neighbor or a wall. Stop after 2000 failed tries in a row. Post the output and write how long it took to run. Photo
-
2
Make it at least ten times faster with a spatial grid. Divide the canvas into cells about the size of your largest circle, store each circle in its cell, and only check circles in the neighboring cells. Time it before and after on the same seed. Post the two timings with the output. Photo
-
3
Pack inside a word. Draw a word in a fat font onto an offscreen buffer and only accept points that land on a dark pixel. Add a radius cap so the letters stay readable. Post the result. Photo
-
4
Pack a photo so the circles follow the light. Sample the brightness under each point and map it to the maximum radius, so bright areas get big circles and dark areas get small ones, or the reverse. Use a portrait of someone who's happy for you to post it. Post the output beside the photo. Photo
-
5
Color the circles by a rule and pick the seed you'd keep. By radius, by position, by the photo's color at that point, or by how many neighbors each circle touches. Post ten seeds as a grid and mark the one you'd print. Photo