Mandelbrot Set ASCII Viewer

The Mandelbrot Set is the set of all complex points which, when one iteratively and infinitely applies the function f_c(z)=z^2+c, converge to a value. This simple rule results in stunning complexity and beauty.
Many Mandelbrot Set animations use regularly colored pixels to represent the number of iterations needed at the fractal’s edges to escape converging. Yet this mathematical object can also be represented as ASCII characters — similar to what I did in my Curses Cam post. The characters are chosen according to their opaqueness. A full stop (‘.’) looks lighter than a dollar sign (‘$’), so they represent a smaller or larger number of iterations needed. The order of characters used is taken from this post by Paul Borke.
As there are only 70 characters used, each frame is being rendered twice to determine the minimum number of iterations needed by every point in that frame. Thereby the full visual character range is used.

The characters shown below represent a Mandelbrot Set still. To see the zoom in action, either run the program (listed below) or take a look at this Mandelbrot Set ASCII journey.

      ..................''''''''``"">>II``''''......                          
    ..................''''''''``^^,,ii::^^``''''......                        
  ..................''''''''``^^::ww$$++,,````''''......                      
................''''''''``^^^^""::$$$$$$::""^^``''''......                    
..............''''''````""{{;;XX$$$$$$$$uuUU,,,,""''......                    
............''''``````^^,,rr$$$$$$$$$$$$$$$$<<$$--``........                  
........''``````````^^""LL$$$$$$$$$$$$$$$$$$$$__""``''......                  
..''''''^^!!"""",,""""::__$$$$$$$$$$$$$$$$$$$$$$ll""''........                
''''````^^::__IIYYii::ll$$$$$$$$$$$$$$$$$$$$$$$$pp^^''........                
''``````"";;[[$$$$$$++__$$$$$$$$$$$$$$$$$$$$$$$$$$^^''''......                
``^^^^,,;;>>$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ww``''''......                
"",,,,II$$nn$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$""``''''......                
"",,,,II$$nn$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$""``''''......                
``^^^^,,;;>>$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ww``''''......                
''``````"";;[[$$$$$$++__$$$$$$$$$$$$$$$$$$$$$$$$$$^^''''......                
''''````^^::__IIYYii::ll$$$$$$$$$$$$$$$$$$$$$$$$pp^^''........                
..''''''^^!!"""",,""""::__$$$$$$$$$$$$$$$$$$$$$$ll""''........                
........''``````````^^""LL$$$$$$$$$$$$$$$$$$$$__""``''......                  
............''''``````^^,,rr$$$$$$$$$$$$$$$$<<$$--``........                  
..............''''''````""{{;;XX$$$$$$$$uuUU,,,,""''......                    
................''''''''``^^^^""::$$$$$$::""^^``''''......                    
  ..................''''''''``^^::ww$$++,,````''''......                      
    ..................''''''''``^^,,ii::^^``''''......

The fractal viewer is written in Python 2.7 and works by determining the terminal’s size and then printing a string of according size. This creates the illusion of a moving image, as the terminal will hopefully always perfectly scroll so that only one frame is visible at a time.
In the code’s first non-comment line one can change the complex point at the image’s center, (really, its conjugate, which is partially irrelevant as the set is symmetric along the real axis) the initial zoom value (complex distance above the image’s center), the zoom factor (the factor by which the zoom value gets multiplied after a frame), the total number of frames (-1 means there is no upper limit), the delay between frames (in seconds, can be floating-point) and the color characters used.

The program’s source code may not be particularly easy to read, yet it does its job and only requires seven non-comment lines! The code is shown below, though the .py file can also be downloaded.
To achieve the JavaScript animation linked to above, I wrote a simple Python converter which takes in the fractal renderer’s output and it spits out an HTML page. This converter’s code is not listed, though the .py file can be downloaded. Instructions on how to use the converter can be seen in its source code.


# Python 2.7 Code; Jonathan Frech, 15th and 16th of June 2017
P,Z,F,N,D,K=-.707+.353j,3,.9,-1,.1," .'`^\",:;Il!i><~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$"
import os,time,sys;H,W,S,n=map(int,os.popen("stty size").read().split())+[sys.stdout,0];W/=2
def C(c):
	global m;z,i=0j,-1
	while abs(z)<=2 and i<len(K)-1+M:z,i=z*z+c,i+1
	m=min(m,i);return K[i-M]*2
while n<N or N==-1:h=Z*2.;w=h*W/H;R=lambda:"\n\n"*(n!=0)+"\n".join("".join(C(P-complex(w/2-w*x/W,h/2-h*y/H))for x in range(W))for y in range(H));M,m=0,len(K);R();M=max(M,m);S.write(R());S.flush();Z,n=Z*F,n+1;time.sleep(D)

Multibrot Set

The Mandelbrot Set is typically defined as the set of all numbers c \in \mathbb{C} for which — with z_0 = 0, z_{n+1} = f_c(z_n) and f_c(z) = z^2 + c — the limit \lim\limits_{n \to \infty} z_n converges. Visualizations of this standard Mandelbrot Set can be seen in three of my posts (Mandelbrot Set, Mandelbrot Set Miscalculations and Mandelbrot Set II).

f_c(z)=z^2+cHowever, one can extend the fractal’s definition beyond only having the exponent 2 in the function to be f_c(z)=z^\text{exp}+c with \text{exp} \in \mathbb{R}. The third post I mentioned actually has some generalization as it allows for \text{exp} \in \{2,3,4,5\}, although the approach used cannot be extended to real or even rational numbers.

f_c(z)=z^3+cThe method I used in the aforementioned post consists of manually expanding (a+b\cdot i)^n for each n. The polynomial (a+b\cdot i)^3, for example, would be expanded to (a^3 - 3 \cdot a \cdot b^2) + (3 \cdot a^2 \cdot b - b^3) \cdot i.
This method is not only tedious, error-prone and has to be done for every exponent (of which there are many), it also only works for whole-number exponents. To visualize real Multibrots, I had to come up with an algorithm for complex number exponentiation.

f_c(z)=z^4+cLuckily enough, there are two main ways to represent a complex number, Cartesian form z = a+b\cdot i and polar form z = k\cdot e^{\alpha\cdot i}. Converting from Cartesian to polar form is simply done by finding the number’s vector’s magnitude k = \sqrt{a^2+b^2} and its angle to the x-axis \alpha = \mbox{atan2}(\frac{a}{b}). (The function \mbox{atan2} is used in favor of \arctan to avoid having to divide by zero. View this Wikipedia article for more on the function and its definition.)
Once having converted the number to polar form, exponentiation becomes easy as z^\text{exp} = (k \cdot e^{\alpha\cdot i})^\text{exp} = k^\text{exp} \cdot e^{\alpha \cdot \text{exp} \cdot i}. With the exponentiated z^\text{exp} in polar form, it can be converted back in Cartesian form with z^\text{exp} = k^\text{exp} \cdot (\cos{(\alpha \cdot \text{exp})} + \sin{(\alpha \cdot \text{exp})} \cdot i \big).

f_c(z)=z^5+cUsing this method, converting the complex number to perform exponentiation, I wrote a Java program which visualizes the Multibrot for a given range of exponents and a number of frames.
Additionally, I added a new strategy for coloring the Multibrot Set, which consists of choosing a few anchor colors and then linearly interpolating the red, green and blue values. The resulting images have a reproducible (in contrast to randomly choosing colors) and more interesting (in contrast to only varying brightness) look.

f_c(z)=z^6+cThe family of Multibrot Sets can also be visualized as an animation, showing the fractal with an increasing exponent. The animated gif shown below was created using ImageMagick’s convert -delay <ms> *.png multibrot.gif command to stitch together the various .png files the Java application creates. To speed up the rendering, a separate thread is created for each frame, often resulting in 100% CPU-usage. (Be aware of this should you render your own Multibrot Sets!)

f_c(z)=z^10+cTo use the program on your own, either copy the source code listed below or download the .java file. The sections to change parameters or the color palette are clearly highlighted using block comments (simply search for ‘/*’).
To compile and execute the Java application, run (on Linux or MacOS) the command javac multibrot.java; java -Xmx4096m multibrot in the source code’s directory (-Xmx4096m tag optional, though for many frames at high quality it may be necessary as it allows Java to use more memory).
If you are a sole Windows user, I recommend installing the Windows 10 Bash Shell.

Multibrot animation (probably loading...)


// Java 1.8 Code
// Jonathan Frech, 11th of September 2016
//          edited 17th of April     2017
//          edited 18th of April     2017
//          edited 20th of April     2017
//          edited 21st of April     2017
//          edited 22nd of April     2017

Continue reading

Bifurcation Diagram

Generating the famous fractal, which can be used to model populations with various cycles, generate pseudo-random numbers and determine one of nature’s fundamental constants, the Feigenbaum constant \delta.
The fractal nature comes from iteratively applying a simple function, f(x) = \lambda \cdot x \cdot (1-x) with 0 \leq \lambda \leq 4, and looking at its poles.
The resulting image looks mundane at first, when looking at 0 \leq \lambda \leq 3, though the last quarter section is where the interesting things are happening (hence the image below only shows the diagram for 2 \leq \lambda \leq 4).
From \lambda = 3 on, the diagram bifurcates, always doubling its number of poles, until it enters the beautiful realm of chaos and fractals.

Bifurcation Diagram lambda in range [2; 4]
For more on bifurcation, fractals and \delta, I refer to this Wikipedia entry and WolframMathworld.


# Python 2.7.7 Code
# Jonathan Frech, 24th of March 2017

Continue reading

Menger Sponge II

In July of 2015 I published my Menger Sponge post. As I said there, the true Menger Sponge is a three-dimensional object, but due to the lack of 3D-integration in pygame, I only showed one of the six cube’s faces. The two-dimensional fractal is officially called Siepinski carpet while the three-dimensional object is really called a Menger sponge.
To achieve the three-dimensional cube, I used Processing 3 together with its Python Mode.
The actual fractal is colored with a pseudo-randomly chosen color. All its smaller cubes then get a slight color shift. The cube rotates slowly, with a different speed on each of the three axes.

Controls

  • Space will advance the cube’s fractalness.
  • ‘q’ will save an image of the current fractal’s state.

Tier 0 Tier 1 Tier 2 Tier 3 Tier 4


# Python Code (Using Processing 3.2.1)
# Jonathan Frech 31st of August , 2016
#         edited  6th of October, 2016
#         edited  7th of October, 2016

Continue reading

Sierpinski TIrangle

Using the same method used in my previous Sierpinski Triangle program, which is written in Python, I wrote a fractal generator for my graphing calculator TI-84 Plus in BASIC.

Faintly visible Somewhat visible Strongly visible


"// TI-84 Plus BASIC Code"
"// Jonathan Frech 25th of April, 2016"
"//         edited 21st of May  , 2016"

Continue reading

Koch Snowflake

In my collection of programs generating fractals this famous one cannot miss.
The Koch snowflake is generated by starting with an equilateral triangle. Every side of the triangle then gets cut into three equal pieces and the center one gets replaced with yet another equilateral triangle.
To get the perfect fractal, you would need to repeat this process infinitely many times.
More information on the snowflake can be found in this Wikipedia entry.

Controls

  • F1 iterates the fractal
  • F2 zooms in
  • F3 zooms out
  • F4 resets zoom
  • F5 takes a screenshot
  • Arrow keys move the camera around

Koch Snowflake


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 25th of April, 2016
#         edited 26th of April, 2016
#         edited 29th of April, 2016

Continue reading

Haferman Carpet

The Haferman Carpet is a fractal, which kind of looks like a woven carpet. To generate it, you start with a single black pixel and apply in each cycle a set of rules.
In each generation every pixel in the carpet will be replaced by nine pixels according to the rules. A black pixel is represented by a 0, a white one by a 1.

The rules

  • 0 \rightarrow \left( \begin{array}{ccc}1&0&1\\0&1&0\\1&0&1\end{array} \right) \text{and } 1 \rightarrow \left( \begin{array}{ccc}0&0&0\\0&0&0\\0&0&0\end{array} \right)

6 iterations of the Haferman Carpet


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 26th of February, 2016

Continue reading

Mandelbrot Set

Working with complex numbers and iterating the function f_c(z) = z^2 + c, I created this program which lets you explore the depth of this self-repeating fractal. The different colors are caused by different color schemes.

Controls

  • Left mouse click and mouse movements control the zoom frame
  • Middle mouse click zooms in
  • Space takes a screenshot
  • F1 moves a zoom back
  • F2 zooms to deafult zoom

Mandelbrot set from afar [-6.75, -4.5] to [6.75, 4.5] An eye in the Mandelbrot set [-0.17466172461436666, -1.0720840874732396] to [-0.1746617232046618, -1.0720840865334365] A gap in the Mandelbrot set [-0.16101577503429365, -1.0369693644261544] to [-0.16019958847736634, -1.0364252400548697]Another gap in the Mandelbrot set [0.25, -0.04999999999999982] to [0.40000000000000036, 0.04999999999999982]A spiral in the Mandelbrot set [0.18276898469650216, -0.5821251321730682] to [0.18311316336591232, -0.5818956797267948]


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech  4th of December, 2015
#         edited  5th of December, 2015
#         edited 11th of December, 2015

Continue reading

Menger Sponge

Normally the Menger Sponge is a three-dimensional cuboid. This program works in a two-dimensional space, so it only shows one side of the sponge.
Because a computer has its capacities, I capped the maximum square count to 10,000. After that maximum, the face will not be complete.

Controls

  • Space divides every square once again

Iteration 0 Iteration 1 Iteration 2 Iteration 3 Iteration 4


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 20th of July, 2015
#         edited 22nd of July, 2015

Continue reading

Sierpinski Triangle

Having recently heard something about chaos theory, I was interested in the sierpinski triangle. Specifically in the random generation. To proof, if it can really be generated using a dice, I wrote this program.

How it works

  • You start with any point inside the triangle (I chose the center)
  • A triangle has three points, let us call them A, B and C
  • You choose randomly between one of those three points, go half the distance and set a pixel right there
  • Repeating this process over and over again (always starting at your last point), you eventually get the sierpinski triangle

Controls

  • Space toggles if the triangle is generated
  • m toggles if the current position is shown (indicated by a blue circle)

Starting with an empty triangle Slowly but surely... Finished sierpinski triangle


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 15th of July, 2015

Continue reading