Newton’s Fractals – Generating Images from Roots

Fractals have always amazed me. Every time I see a fractal, I can get just as excited as the first time I saw the mandelbrot set. In this post, we’ll go through a method, which can generate beautiful structures from simple functions.

Link to the source code for this project: https://github.com/SSODelta/newtons-fractals

For a real-valued function $f(x)$, a root is any $x$, which satisfies $f(x) = 0$. A trivial case might be $x=0$ for the function $f(x) = x^2$. Roots are also what is found with the famous solution $\frac{-b \pm \sqrt{D}}{4\cdot a \cdot c}$ to the quadratic equation $f(x) = a \cdot x^2 + b \cdot x + c$.

Sometimes however, a second degree equation does not have any real solutions. That is when $D < 0$. A quadratic equation will, however always have two solutions in the complex plane.

The complex plane is an extension of the real number line to a plane. The major new addition is the introduction of the imaginary unit $i$, which is the unique number satisfying $i^2 = -1$. This allows us to find solutions to equations such as $x^2 = -1$, which otherwise wouldn’t have any real solutions. In the complex plane every single point has a unique complex number associated with it. A complex number consists of two parts, a real part and an imaginary part. This is usually stylized $a + b \cdot i$, where $a$ is the real part and $b$ is the imaginary part.

What does all this have to do with fractals?

To make a fractal, we need to assign a color code to each point in a plane, in this case the complex plane as discussed above. We decide on an initial size $(w, h)$ and a resolution $r$. The size is the width $w$ and height $h$ of an image measured in pixels. The resolution $r$ is the “zoom-factor”, if you will. If $r=1$, then we’ll deal with complex numbers $a + b \cdot i$ in the range $a,b \in [-1, 1]$. And in general $a,b \in [-r, r]$. To make it clear how to combine roots and points in the complex plane to make fractals, we need to introduce a very important method.

Introducing, Newton’s approximation for finding roots to a real-valued function (although it extends nicely to the complex plane, fortunately). For a function $f(x)$ and an initial “guess” $x_0$, a better guess will be $x_1 = x_0 - \frac{f(x_0)}{f'(x_0)}$, where $f'(x)$ is the derivative of $f(x)$. In fact, this can be generalized to the recursive formula $x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}$. So how do we make fractals?

• Have a function $f(x)$, a size $(w, h)$ and a resolution $r$.
• Assign a unique color to each root to $f(x)$.
• For every point $P(x, y)$ in the image, make an inital guess $x_0 = (-r + (\frac{x}{w}) \cdot 2r, (-r + \frac{y}{h}) \cdot 2r)$.
• Use Newton’s approximation method to approximate a root as long as $x_{n+1} - x_n > \epsilon$.
• Color the pixel $P(x, y)$ the color which is assigned to the root which has just been approximated.

In practice, we’ll only deal with polynomials, though, because they’re easy to calculate and to calculate the derivative of.

Using this method gives us some stunning images:

Link to an album with more than 100 HD fractals: http://imgur.com/a/xOFyF#0

As a little extra feature I have also colored the points which converge faster darker as to create some variation in the images.

Creating Colored Images from Regular Expressions

Note: This post creates some cool colors, but to see something truly stunning, check out dln385’s images, inspired by these posts.

In my last post, I detailed an algorithm for generating images using regular expressions. However, I found them a tad boring and mundane, so in this post I’m going to add colors to the images.

We follow exactly the same algorithm for choosing where to color the pixels, but we need a method to decide what color to paint. To accomplish this, we introduce three colors channels C, M, Y which are basically a long string, composed of the same alphabet as the areas (0, 1, 2, 3).

For each string S (area) do the following:

• If S matches the regular expression, mark it as painted.
• Define a blank color CMY(0, 0, 0).
• For each color channel, xor the string accompanying the channel, and if it matches the regular expression, change the color channel from 0.0 to 1.0.

Let’s do an example. Let R be the regular expression (1+2+3)*0(1+3)*0(0+1+2+3)*.

Let the following be the color channels C,M,Y:

• C – 032102
• M – 033122
• Y – 132212

If we test the string S = 330100, we find it matches, so we must paint it.

Calculate S XOR C = 330100 XOR 032102 = 302002, which matches the regular expression, so the cyan color channel is 1.0.

Calculate S XOR M = 330100 XOR 033122 = 303022, which also matches the regular expression, so the magenta color channel is also 1.0.

Calculate S XOR Y = 330100 XOR 132212 = 202321, which does not match the regular expression, so the yellow color channel is 0.0.

This means we paint the area, marked by the string 330100 with the CMY-color (1, 1, 0).

Here is a visual example to showcase this new method:

You can also do AND instead of XOR, it produces images like the following:

It’s obviously less visually stunning, but still interesting. Here is OR instead:

It’s also less visually diverse, but still cool to look at.

Generating Images from Regular Expressions

There is an update to this post, which includes colors: https://ssodelta.wordpress.com/2014/03/25/creating-colored-images-from-regular-expressions/

Source code complete with Javadoc can be found at: http://pastebin.com/ccRK75aq

This post will detail a method for using regular expressions to generate images.

A regular expression is an array of characters, which represents a series of instructions, which for any input either matches or doesn’t match. For a simple example, the regular expression

ssodelta

Only matches the string ssodelta. This makes sense, but we can do pretty nice things with regular expressions. If we for example write

ssodelta|delta

Then the regular expression either matches ssodelta or the string delta. This is because the vertical bar | represents the symbol or. We also have so called quantifiers, the most important of which is the kleene star *.

sso(delta)*

The parentheses group strings together, like regular parentheses in mathematical expressions (2+4)*3, for example. The kleene star * means zero or more, meaning that there can be zero or more delta and it will still match the regular expression. A few matches include ssodelta, ssodeltadelta, ssodeltadeltadelta and so on…

I won’t completely detail regular expression, but if want to read up on it, I suggest close-reading the wikipedia article on the matter, but the bottom line is that regular expressions are really cool. But how can they be used to generate images?

Consider a completely square image, which is divided into 4 quadrants:

We can refer to each of these four quadrants by their numbers 0,1,2,3, and we can further divide these quadrants into four numbers, and label them accordingly.

But what does this have to do with regular expresssions? Well, we can have a regular expression, which only uses the characters 0,1,2,3 and every string adresses an area in this area. For example, the string 022 represents the area marked with red:

We can decide on a resolution n, and generate all strings of length n, parse them through a regular expression, and if it matches, paint it black. For example, the regular expression (0|2)(0+1+2+3)* generates the following image (for any resolution >= 2):

This is because the regular expression only matches if the cell begins with a 0 or a 2, which is why the 0th and the 2nd quadrants are painted black, and the rest aren’t.

We aren’t limited to boring checkers though, if we feed the generator the regular expression (1+2+3)*0(1+3)*0(0+1+2+3)*:

Notice the self-similarity in the image, that’s because this image is actually a fractal.

Here is a gallery of other images generated using this technique:

Notice how often triangles appear in these images.

Although the .java file has an included Javadoc, here’s how you use it to generate images:

Generator g = new Generator(RESOLUTION, IMAGE_SIZE);
g.generateAndExport("REGULAR_EXPRESSION", "EXPORTED_FILE_PATH.png"); //Must be a .png file

If you prefer Python code, check out Jack Morris’ post on the same subject, where he implements it in Python: http://jackm.co.uk/posts/2014/03/25/Using-Regular-Expressions-To-Generate-Images/