The Math Behind the String Art
How does a computer know how to recreate a portrait using only a single, continuous thread? It’s not magic—it’s a combination of geometry, integral calculus principles, and a "greedy" optimization algorithm.
The Core Principle: Subtractive Reconstruction
Instead of thinking about where to add black lines, the algorithm looks at the original image and tries to "erase" the darkness by placing thread over it. This is an iterative process that follows these logic steps:
- Line Scoring: From the current pin, the computer "scans" all possible paths to other pins. For every path, it calculates the average darkness of the pixels underneath it. This is essentially a discrete version of a Line Integral.
- Selection: It chooses the darkest possible path—the one that represents the most information from the original photo.
- Subtraction (The Fade): Once a line is "drawn," the algorithm lightens the pixels along that path in its internal memory. This tells the computer: "This part of the image is already represented by a thread, don't focus on it too much anymore."
- Iteration: The end of the chosen line becomes the starting point for the next search. This continues for thousands of steps (usually 2,000 to 4,000) until the image emerges.
Is it a Radon Transform?
If you are familiar with medical imaging (like CT scans), you might recognize the similarity. A CT scan uses the Radon Transform to reconstruct a 3D body from 2D projections. While our algorithm uses the same fundamental math—calculating the density of a line—it works in reverse. It is an Iterative Matching Pursuit that approximates your photo by picking the best "chords" from a circular dictionary of possibilities.
Choosing the Right Photo
The algorithm is clever, but it has its limitations. For the best results, keep these "physics" of string art in mind:
- Edge Connectivity: The generator works best on images where the dark areas reach or come close to the edges of the circle. Since every thread must start and end at the perimeter, the algorithm needs "entry points" to travel through.
- The "Middle" Problem: If your image has a dark object only in the very center with a pure white background around it, the algorithm will struggle. To reach the center, it must pull lines through the white areas, creating unwanted "ghost" lines that blur the final result.
- Balanced Contrast: High-contrast images might look good, but subtle gradients often produce more photorealistic results. If there is a dark frame or a shadow near the pins, the algorithm can use those areas to "turn" and maneuver without leaving visible marks across the entire composition.
A good and a bad image for generating pattern.
Display artifacts: Your screen and this webpage also have limited resolution so the shown picture can have patterns like the below image has. I generated it from a solid black image, the patterns that you see only emerged due to the limited resolution. You won't see this pattern in the reality. Maybe it's a Moiré pattern, but I'm not sure.
Artifacts on the pattern generated from a solid black image.