Plotting Polynomials

16 Oct 2021 ∞

The first render that worked

Earlier this week I got nerd-sniped by a video about Newton's Fractal, which covered Fun Facts about finding the roots of polynomials that result in a fractal pattern. While watching the video I got caught up in the movement of how a point finds its root, because where there's movement there's something to plot.

One of the nice things about 3Blue1Brown is all of the videos are generated from code, and that code is open source, so I figured I could probably implement the algorithm myself. Eventually I was right, but it took a while to make it all work.

The image to the right is the first render that behaved as expected. I was reimplementing a bunch of methods from JavaScript, most of which used odd syntax I wasn't familiar with. I managed to get most of them working, but the one that ended up causing the most trouble was a method that returned all combinations of the roots. My version never worked correctly, however someone pointed out that what I was trying to do is implemented in Swift Algorithms, so I used that instead and everything came together.


After I had the math part done I could turn my attention to the graphics part. I started off with simple black lines, but one of the more interesting elements of the fractal is which root a points converges on. With this in mind I added colors to the lines, tracing the paths to each root value.

Drawing both the paths and just the pixels at a high enough density shows the fractal emerging, although the path version gets a little too bright with that many lines.

A 6k render

A more reasonable density of lines creates a really nice nebula effect, with most of the paths going towards their closest root, but not all. The overlap produces striking bolts of light between roots.

I'm planning on rendering some for plotting, but first I need to decide whether I'm going to plot them in one color, or try to follow the digital versions and use different colors for each root.

art generative swiftgraphics swift