You've probably seen this figure a million times. ![[mandelbrot.jpg]] This is the Mandelbrot set, named after Benoit Mandelbrot, who introduced fractals to the world with his famous 1967 paper, *How Long Is the Coast of Britain?* While the surface area of this shape is bounded, its edge is infinitely long, much like that of the coast of Britain. No matter how much you zoom in onto the edge, you're going to find more and more roughness, subshapes of subshapes that have a length of their own. It never ends. The Mandelbrot set is described by the following equation. $ \begin{align} f_c(z)_{n+1}=z_n^2+c \end{align} $ You define if a complex number $c$ belongs to the set by apply the formula over and over to it. If the number is stable, meaning that it's bounded and doesn't reach out towards infinity as we apply the calculation times and times again, the point belongs to the set. Otherwise, it does not. The interesting part happens near the edge of the set (the shape in the middle). As we consider even the tiniest variations of numbers that live next to each other, we get widely different results. This is why the set is related to chaos: Any change, even of the smallest account, can lead to a drastically different outcome. Numbers that live near the edge of the set, no matter how close, do or do not belong to the set, resulting in that infinite edge which in the above illustration appears as the brightest part. Those bright point do not belong to the set, they define its contour. Before diving into drawing the set ourselves, we first need to make sure we understand what complex numbers are. Many implementations out there skip that entirely and apply premade recipes without getting into the math, which I'd like to address here as it will give you a firm understanding of what the set is and what the formula does. The math is really not that complex... apart for the fact it deals with complex numbers. 👨‍💻 **WORK IN PROGRESS** ___ ## Notes to Self ## Complex numbers Complex numbers are composed of a real part and an imaginary part. You can think of them as a point on a $2d$ plane with an $x$-axis and a $y$-axis. The real part would be the $x$-coordinates of that point, and the imaginary part would be the $y$-coordinates. The imaginary part comes from $\sqrt{-1}$. It's not possible for a square root to yield $-1$ when dealing with numbers on the real number line. A number that has a negative square root in it is said to have an imaginary part, otherwise this is a real number living on a one dimensional line with its $y$-coordinates being $0$. The imaginary part is denoted $i$, which is equal to $\sqrt{-1}$. Consider for instance $\sqrt{-76}$. You can rewrite it $\sqrt{-1 \cdot 76}$, and then $i\sqrt{76}$. The coordinates of our number on the complex plane are $(0, \sqrt{76})$. magnitude ## Operations of Complex Numbers $i2$ addition substraction multiplication powers ## Understanding the Equation ## Implementation We recursively apply a function to an input. The Mandelbrot set is drawn by iterations, we take pixels and map them onto a 4x4 grid The way you pick the values to input into the function is that you map your two-dimensional system pixel-width onto a 4x4 coordinate system that goes from -2 to 2 on both axis. Examples of cyclic and unbounded behavior code ## Graphics Debugging inversed y-axis: https://gemini.google.com/app/36fc042f0a9abcc0 Hard to catch because of vertical symmetry. ### Iterations ### Coloring https://gemini.google.com/app/36fc042f0a9abcc0 ## Optimization Linear interpolation repeated calculation Powers Parallelism Graphics card? ## Pan & Zoom This is the main reason we've been concerned with optimization, after all. ## Display coordinates grid ## 3D ## Going further Julia set It produces complex numbers with an imaginary part. As soon as the input becomes unbounded, we draw that point. A point is unbounded as soon as it gets bigger than $2$ or $-2$, because we know for a fact that all points outside that range are unbounded. In two colors, the Mandelbrot set is the representation of all the points that are unbounded. The shape left in the middle is the points that did not get drawn, because they never became unbounded; they follow a cyclic pattern. One can add gradient colors, to represent the generation at which the number became unbounded. 2 is the magnitude, but we never really get a non complex number. Operations of complex numbers produce complex numbers. ## Operations of complex numbers: ### Subtraction Let $z_1=a+bi$ and $z_2=c+di$. $ z_1-z_2=(a-c)+(b-d)i $ ## Powers Let $z=a+bi$. Remember that $i=\sqrt{-1}$ and $i^2=-1$. You can get rid of $i^2$, but you keep the $i$ for the imaginary part. Can they evaluate to $0$? Like $z=5+0i$? $ \begin{align} z^2=(a+bi)^2=(a+bi)(a+bi)=a^2+2abi+b^2i^2=a^2+2abi-b^2=(a^2-b^2)+(2ab)i \end{align} $ ### Magnitude A complex number's magnitude (distance from the origin) is also called its absolute value or modulus. This corresponds on its distance from the origin $(0,0)$ on the two dimensional plane. It's calculated like so: $|z|=\sqrt{a^2+b^2}$. Let us use the complex number $z=-7+24i$. $ \begin{align} |z|=\sqrt{(-7)^2+(24)^2}=\sqrt{49+576}=\sqrt{625}=25 \end{align} $ The magnitude of $z$ is $25$, which we can write as $|z|=25$. Plan: Why this article: Most articles and videos don't really explain the math and formulas behind drawing the set and operations of complex numbers. We get from the famous equation to mumbo jumbo that you have to believe works. What are complex numbers Operations of complex numbers Magnitude Mandelbrot set Significance Drawing with SFML Optimization Zooming Make it 2 articles? One for the set, one for the C++ part? Auto Zoom: Detecting contrasts and do an election instead of pinpointing a fixed point? [Compile SFML on Mac on the command line (Shown on Apple M1) -- No XCode](https://www.youtube.com/watch?app=desktop&v=zjv4aGzFous) https://www.sfml-dev.org/tutorials/3.0/window/window/#opening-a-window [What's so special about the Mandelbrot Set?](https://www.youtube.com/watch?v=FFftmWSzgmk) [The Boundary of the Mandelbrot Set](https://www.youtube.com/watch?v=Oh1AiiPpoTo) https://www.mi.sanu.ac.rs/vismath/javier/b2.htm https://complex-analysis.com/content/mandelbrot_set.html https://www.youtube.com/shorts/STRq1-4Xemw [Buddhabrot](https://en.wikipedia.org/wiki/Buddhabrot) https://en.wikipedia.org/wiki/Plotting_algorithms_for_the_Mandelbrot_set