Mandelbrot Set Renderer
Here's a somewhat basic rundown of a simple program I made whilst bored in class which can render the Mandelbrot Set on a terminal using Python.
Of course, a clearer and more comprehensive image could be obtained using Turtle graphics, but I chose to write this program without it, as I thought this rendering method looked way cooler.
The Mandelbrot Set is a famous - if not the most famous - fractal. It contains infinite detail, and can be viewed in as small a scale as wanted without ever losing it's complexity (at the expense of longer rendering times for computers.)
Here's a picture of The Mandelbrot Set:
You may notice this picture is black-and-white, instead of the usual colour images you may see. I'll touch on this later.
The Mandelbrot Set lies on the complex plane - where complex number c is iterated with the formula z = z² + c starting with z = 0. If the value does not diverge to infinity after n iterations, it is defined as being within The Mandelbrot Set. The higher n iterations is, the closer of a shape you get to the true Mandelbrot Set, which can only be achieved after infinite iterations. Since infinite iterations is not computable, n tends to be a fairly large number, depending on the level of accuracy wanted.
Though complex numbers can be handled with the cmath library, the formula required for The Mandelbrot Set can be expressed without this library as well, in pseudocode shown:
(I nicked this off of Wikipedia)
This is a simple, naive "Escape Time" algorithm, and is the most commonly used for its ease to program. It iterates through the formula n amount of times, either breaking the loop once the value escapes a certain threshold (not in the set), or finishing the loop - meaning the value remains bounded (in the set).
Now, in Python:
I went ahead and created some code to print x amount of characters on a line, and print y lines. I then assigned the coordinates to each character printed, starting with -2, 1 for the first character on the first line, and increasing the x coordinate by 4/x as the characters are drawn on the first line, till the last character has the coordinates 2, 1. The code above is performed on each character, with x and y as inputs, and the character is either assigned as a "#" or " ", depending on whether or not it is in the set. This then continues for the next row, but with the y being decreased by 2/y.
For the displayed plane, it took time to figure out what the scale, and position of it should be, but I managed to get a decent image after experimenting for a bit.
Output in terminal at 100 iterations
Most images of The Mandelbrot Set, you may notice, have colour. The colour for each pixel is determined by how many iterations are required them to diverge. The number then corresponds to the hue of the pixel, with black (usually) for pixels that remain bounded, like below:
I then rewrote my program to assign certain Unicode characters to the iterations achieved per pixel/character. This is the resulting image:
After some more rewriting, the program can now allow you to input the x and y coordinates of the viewer, along with iterations and scale. This allows for you to view the more complex structures within the fractal, resulting in renders such as this: