Geometry of string art

Picture of the lab member

Jay Chavda

MS Student

2025-02-27

What is string art?

String art is a technique that involves weaving threads between fixed points/nails to form patterns by playing with the manner in which threads intersect each other. These fixed-points/nails are typically arranged along the edge of a circular frame and by connecting specific nails with threads that travel along straight lines, the overlap can be tuned, gradually building up a recognizable image or a pattern. This art form was invented in the late 19th century by Mary Everest Boole, an English mathematician. She came up with this technique as an educational tool to help children understand mathematical concepts such as geometry through hands-on learning.

What makes string art particularly interesting is its use of simple geometric shapes and linear structures to approximate complex forms. Each thread is just a straight line, but by layering many lines at different angles, intricate shapes and gradients can emerge. This process can be thought of as the construction of an image through a series of basic building blocks. In this blog post, we will delve into the mathematical ideas behind string art. We will examine how geometry and linear algebra play a crucial role in determining the best arrangement of threads to approximate a given image. Additionally, we will provide a step-by-step explanation of an algorithm that automates the process, making it easier to design and recreate string art using computational tools.

String art configurations with 8, 13, and 18 nails. Here each nail along the circle is connected to every other nail. We see that by increasing the number of nails leads to complex geometric patterns, demonstrating the ability of string art to approximate intricate shapes through simple linear elements.

Mathematical foundation

Representation of threads as basis functions

Each thread is defined as a line segment connecting two points on the circle. Mathematically, the equation of a line between two points (x0,y0)(x_0, y_0) and (x1,y1)(x_1, y_1) is given by: r(t)=(1t)[x0y0]+t[x1y1],t[0,1].\vec{r}(t) = (1 - t) \begin{bmatrix} x_0 \\ y_0 \end{bmatrix} + t \begin{bmatrix} x_1 \\ y_1 \end{bmatrix}, \quad t \in [0, 1].

A visual representation of the thread intensity matrix TT for a single thread (denoted by blue line) connected between two nails(denoted by red dots). The pixels which have an intersection with the thread are assigned value Tij=1T_{ij}=1, while the other pixels of the grid are assigned values Tij=0T_{ij}=0.

Thread intensity matrix

Each thread can be represented as a binary vector, where each entry corresponds to a pixel on the grid. If the pixel lies on the thread, the entry is 1; otherwise, it is 0. Collecting all such thread vectors forms the thread intensity matrix, TRn×mT \in \mathbb{R}^{n \times m} with nn being the total number of pixels in the image grid, mm is the total number of threads, determined by the number of connected nails. If there are kk nails with all of them connected, then m=k(k1)m = k(k-1), as each pair of nails represents a unique thread. Each column of TT corresponds to a thread, and each row corresponds to a pixel in the grid. The value TijT_{ij} is 1 if thread jj passes through pixel ii, and 0 otherwise.

Constructing TT

For each thread connecting nails at (x0,y0)(x_0, y_0) and (x1,y1)(x_1, y_1),

For a small grid and few threads, the thread intensity matrix may look like this: T=[100110011].T = \begin{bmatrix} 1 & 0 & 0 & \cdots \\ 1 & 1 & 0 & \cdots \\ 0 & 1 & 1 & \cdots \\ \vdots & \vdots & \vdots & \ddots \end{bmatrix}. Here each row corresponds to a pixel and each column corresponds to a thread. The ntries are 1 if the thread passes through the pixel. By constructing TT, we create a mathematical representation of the threads' contributions to the image grid. This matrix serves as the basis for solving the linear system to reconstruct the target image.

Image reconstruction as a linear system

The reconstruction of an image in string art involves finding the optimal combination of threads that approximates the target image. Let us define the components and we will explain the reconstruction process in detail after the definitions.
Target image, I\vec{I}: The target image is flattened into a vector I\vec{I} of size n×1n \times 1, where each entry represents the grayscale intensity of a pixel. The goal is to approximate I\vec{I} using the contributions of the threads.
Thread weights, w\vec{w}: The vector w\vec{w} of size m×1m \times 1 contains the weights of the threads. Each weight wjw_j determines the intensity or contribution of the thread jj to the reconstruction. These weights capture the importance of a specific feature in the image we would like to reconstruct and will become important soon.
Linear system representation: We can immediately see that by multiplying TT and w\vec{w}, we get a flattened version of an image. The multiplication here simply telling us that the image I\vec{I} is constructed by a sum of threads from TT with weights w\vec{w}. The reconstructed image is given by, Ireconstructed=Tw,\vec{I}_{\text{reconstructed}} = T \vec{w}, where TT is the thread intensity matrix of size n×mn \times m, and w\vec{w}, is the the weight vector of size m×1m \times 1. The objective is to find w\vec{w} such that the reconstructed image TwT \vec{w} closely matches the target image I\vec{I}.

Minimizing reconstruction error:

Our goal is to construct a target image by a set of threads. The error between the constructed and target image is measured using the Euclidean norm give by, E(w)=TwI22.\mathcal{E}(\vec{w}) = \|T \vec{w} - \vec{I}\|_2^2. The optimal weights w\vec{w}^* minimize this error for a given TT, w=argminwTwI22.\vec{w}^* = \arg \min_{\vec{w}} \|T \vec{w} - \vec{I}\|_2^2. This is a standard least squares problem, where the solution ensures the best possible approximation of I\vec{I} in terms of the thread contributions. By solving this optimization problem, we determine how much each thread contributes to the final reconstructed image.

Solving the optimization problem

The problem of minimizing the quadratic error, E(w)=TwI22\mathcal{E}(\vec{w}) = ||T \vec{w} - \vec{I}||_2^2 can be solved mathematically using linear algebra. In our application, TT is the thread matrix composed of column vectors representing the contribution of each thread (or line) in the image, and I\vec{I} is the flattened target image.

Objective function

The quadratic/squared error can be written as, E(w)=TwI22=(TwI)(TwI).\mathcal{E}(\vec{w}) = \|T \vec{w} - \vec{I}\|_2^2 = (T \vec{w} - \vec{I})^\top (T \vec{w} - \vec{I}). Expanding the product gives, E(w)=wTTw2ITw+II.\mathcal{E}(\vec{w}) = \vec{w}^\top T^\top T \vec{w} - 2 \vec{I}^\top T \vec{w} + \vec{I}^\top \vec{I}. Since II\vec{I}^\top \vec{I} is a constant and does not depend on w\vec{w}, it can be ignored during minimization.

Gradient of the objective function

To find the optimal weight vector w\vec{w} that minimizes E(w)\mathcal{E}(\vec{w}), we set the gradient with respect to w\vec{w} equal to zero, wE(w)=2TTw2TI=0.\nabla_{\vec{w}} \mathcal{E}(\vec{w}) = 2T^\top T \vec{w} - 2T^\top \vec{I} = \vec{0}. Dividing by 2, we get the normal equation, TTw=TI.T^\top T \vec{w} = T^\top \vec{I}. If TTT^\top T is invertible, the optimal weight vector w\vec{w}^* can be computed as, w=(TT)1TI.\vec{w}^* = \left( T^\top T \right)^{-1} T^\top \vec{I}. In cases where TTT^\top T is singular or ill-conditioned, the Moore-Penrose pseudoinverse provides a robust alternative, w=T+I,\vec{w}^* = T^{+} \vec{I}, with T+T^{+} denoting the pseudoinverse of TT.

Reconstruction

Once the weight vector w\vec{w} is obtained, the reconstructed image is formed as, Ireconstructed=Tw.\vec{I}_{\text{reconstructed}} = T\vec{w}. Reshaping Ireconstructed\vec{I}_{\text{reconstructed}} back into the original grid produces the final string art representation.
This approach illustrates how linear algebra and numerical optimization methods can be harnessed to approximate complex images using simple geometric primitives.

Reconstructed string art from an input PNG image using the technique described here. The left side shows the target image I\vec{I}, while the right side demonstrates its reconstruction Ireconstructed\vec{I}_{\rm reconstructed} using weighted thread patterns.

Conclusion

String art beautifully combines art and mathematics. By understanding its geometric and algebraic principles, you can create stunning reconstructions of complex images. Do experiment with different configurations, numbers of nails, and target images to unleash your creativity with the python code attached here!