ALGORITHM7 MIN READ

Greedy Radial Search Math Explained: How String Art Algorithms Work

By Karthick Subramanian ·

The first time you watch a digital string art preview assemble a recognizable human face out of nothing but straight lines, it feels almost like a magic trick. There are no curved strokes, no airbrush gradients, and no variable line widths—just one continuous taut thread hopping from nail to nail around a wooden ring. Behind that illusion sits a surprisingly approachable piece of math called greedy radial search. Once you understand how the algorithm thinks, every slider in the studio—pin count, line count, thread weight, and contrast—suddenly makes intuitive sense.

Side-by-side comparison of an original portrait photo and its radial chord string art reconstruction.

1. Turning Your Photograph Into a Darkness or Color Budget

Before the generator picks a single pin, it crops your photograph into a square, masks everything outside the circular nail ring, and reads the pixel values inside. In classic grayscale mode, bright pixels near pure white need zero thread, while deep shadows around pupils, eyebrows, or dark hair represent high darkness values. Think of that grayscale grid as a target budget: every pixel starts with a number saying how much visual weight it still owes the finished portrait.

In 4-spool CMYK color photo string art, the math splits that budget into four separate error maps—one each for Cyan, Magenta, Yellow, and Black. Undercolor removal shifts neutral dark grays onto the Black spool so Cyan, Magenta, and Yellow only have to carry actual hue. That single step is why skin tones stay warm and natural instead of turning muddy brown after two thousand crossings.

2. Why Straight Chords Create Perceived Shading

A single strand of 40wt polyester thread is very thin—roughly 0.20 millimeters wide—so one pass across a bright cheek barely registers. But when twenty or thirty chords intersect across the same square centimeter, our eyes average the dark thread with the white board peeking between the strands. That optical averaging is identical to how newspaper halftones or cross-hatched pen drawings work.

Because every chord must run straight from one perimeter nail to another, the algorithm cannot draw a short line inside the face without also crossing the pixels leading out to the rim. That constraint is the whole art of string art: finding chords that pass heavily through dark facial features while spending as little ink as possible across bright backgrounds.

3. Scoring Thousands of Candidate Pins at Every Step

Suppose you start at Pin 0 at the top of the circle on a 240-pin board. From Pin 0, there are 239 other nails you could stretch the thread to. However, jumping to Pin 1 or Pin 2 only hugs the outer edge of the circle and creates an ugly dark halo around the frame. So the generator enforces a minimum pin skip—usually around 12 to 20 pins in either direction—leaving roughly 200 valid destination chords at each step.

For every valid candidate chord, the algorithm walks along the straight pixel path connecting the start pin and end pin, adds up the remaining error values under those pixels, and divides by the chord length. Dividing by length is crucial: without length normalization, long diameter chords would always beat shorter corner chords just by touching more pixels. The chord with the highest average error wins the step.

4. Subtracting Thread Weight and Avoiding Repeat Loops

Once a winning chord is chosen, the generator draws it onto the preview canvas and subtracts a fixed fade amount—controlled by your Thread Weight and opacity settings—from every pixel along that path. That subtraction guarantees the algorithm does not get stuck bouncing back and forth between the exact same two nails forever. As soon as the hair or collar gets enough passes, its error values drop toward zero, and the search naturally migrates toward subtler features like eyelids, nostrils, and the corner of a smile.

We also keep a fast lookup set of already-used pin pairs per spool so the exact same chord is never wound twice on top of itself. Physically, stacking two threads on the exact same two nails adds bulk on the nail shank without adding any new cross-hatching detail.

5. Practical Takeaways for Better Portraits

Understanding this greedy loop immediately explains why busy backgrounds hurt portraits: if there are dark trees or patterned walls behind someone's head, those background pixels compete for chords and steal lines away from the face. Cropping tightly and boosting contrast slightly before generating concentrates the entire line budget right where the viewer looks first—the eyes and expression.

Try this in the StringArts Generator

Upload any photo, adjust pins and contrast, and export your PNG, PDF, and TXT winding sequence in seconds.

Try the Free Generator