The Mandelbrot Set is typically defined as the set of all numbers for which — with
,
and
— the limit
converges. Visualizations of this standard Mandelbrot Set can be seen in three of my posts (Mandelbrot Set, Mandelbrot Set Miscalculations and Mandelbrot Set II).
However, one can extend the fractal’s definition beyond only having the exponent
in the function to be
with
. The third post I mentioned actually has some generalization as it allows for
, although the approach used cannot be extended to real or even rational numbers.
The method I used in the aforementioned post consists of manually expanding
for each
. The polynomial
, for example, would be expanded to
.
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.
Luckily enough, there are two main ways to represent a complex number, Cartesian form
and polar form
. Converting from Cartesian to polar form is simply done by finding the number’s vector’s magnitude
and its angle to the x-axis
. (The function
is used in favor of
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 . With the exponentiated
in polar form, it can be converted back in Cartesian form with
.
Using 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.
The 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!)
To 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.
// 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
// import
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.FontMetrics;
// drawer thread class
class drawer implements Runnable {
// frame number
private final int n;
private final int[] colors;
private final int colordepth;
private Thread t;
// initialize and run thread
drawer(int n, int[] colors) {
this.n = n;
this.colors = colors;
this.colordepth = colors.length;
t = new Thread(this, "drawer" + n);
t.start();
}
// return tread
Thread get() {
return t;
}
// thread run function
public void run() {
//System.out.println("Drawing " + n + ".");
draw();
//System.out.println("Drew " + n + ".");
}
// draw a frame
void draw() {
// image
int width = multibrot.imagewidth;
int height = multibrot.imageheight;
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// determine exponent
double exp = multibrot.minexp + (n*(multibrot.maxexp-multibrot.minexp)/multibrot.frames);
// complex plane
double p1y = multibrot.p1y;
double p2y = multibrot.p2y;
double p1x = multibrot.p1x;
double p2x = multibrot.p2x;
double facx = (p2x-p1x)/width;
double facy = (p2y-p1y)/height;
double addx = p1x;
double addy = p1y;
// complex numbers, exponent, iterations
double a, b, _a, _b, asqr, bsqr, c, d, k, alpha;
int i;
// calculate Mandelbrot Set
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
// reset
i = -1;
a = 0;
b = 0;
asqr = 0;
bsqr = 0;
c = x*facx+addx;
d = y*facy+addy;
// condition of escaping the 2-circle
while (asqr+bsqr < 4.0) {
// polar
k = Math.sqrt(asqr+bsqr);
alpha = Math.atan2(b, a);
// polar exponentiation
k = Math.pow(k, exp);
alpha *= exp;
// rectangular
a = k*Math.cos(alpha)+c;
b = k*Math.sin(alpha)+d;
// square to save computational time
asqr = a*a;
bsqr = b*b;
// another iteration needed
i++;
// too many iterations needed, abort
if (i >= colordepth-1) break;
}
// set pixel
img.setRGB(x, height-1-y, colors[i]);
}
}
// draw text (with subscript and superscript!)
int kx = 10;
int ky = 10;
String expstr = Double.toString(exp);
while (expstr.length() < 6) {
expstr += "0";
}
expstr = expstr.substring(0, 4);
Graphics g = img.getGraphics();
g.setColor(multibrot.fontcolor);
g.setFont(new Font("TimesRoman", Font.PLAIN, multibrot.fontsize));
FontMetrics fm = g.getFontMetrics();
String[] eq = new String[] {"f", "c", "(z) = z", expstr, " + c"};
int[] vert = new int[] {0, fm.getHeight()/3, 0, -fm.getHeight()/3, 0};
String str;
for (int t = 0; t < eq.length; t++) {
str = "";
for (int tstr = t; tstr < eq.length; tstr++) {
str += eq[tstr];
}
g.drawString(eq[t], width-fm.stringWidth(str)-kx, height+vert[t]-ky);
}
// save image with appropriate name
String name = Integer.toString(n);
while (name.length() < multibrot.filenamedigitlength) name = "0" + name;
try {
File file = new File(System.getProperty("user.dir") + "/multibrot" + name + ".png");
ImageIO.write(img, "png", file);
}
catch(IOException e) {
e.printStackTrace();
}
}
}
// main class
public class multibrot {
/*
* PARAMETERS
*/
// total number of frames (and there is the 0th frame!)
static final int frames = 1000;
// image
static final int imagewidth = 1000;
static final int imageheight = 1000;
// complex numbers which determine section
// topleft = (p1x) + (p1y)i
// bottomright = (p2x) + (p2y)i
static final double p1y = 2;// 1.2;
static final double p2y = -2;//-1.2;
static final double p1x = -2;//(p2y-p1y)/2.*imagewidth/imageheight;
static final double p2x = 2;//(p2y-p1y)/2.*imagewidth/imageheight;
// from z^0+c to z^maxexp+c
static final double minexp = 0;
static final double maxexp = 10;
// padded length of file name digits
static final int filenamedigitlength = 4;
// font color
static final Color fontcolor = new Color(255, 255, 255);
static final int fontsize = 12;
// main function
public static void main(String[] args) {
// time calculation
long t0 = System.currentTimeMillis();
// thread array
Thread[] T = new Thread[frames+1];
// color
int[] colors = getcolors();
// initialize a thread per frame
for (int n = 0; n < frames+1; n++) {
T[n] = new drawer(n, colors).get();
}
System.out.println("Threads initialized.");
// join threads
for (Thread t : T) {
try {
t.join();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
// calculate time, print
long deltat = (System.currentTimeMillis()-t0)/1000;
double perframe = (double) deltat / frames;
System.out.println("Took " + deltat + " seconds for " + frames + " frames (" + perframe + " s/f).");
}
// get color information
private static int[] getcolors() {
// colors
double[][] colors = new double[256][];
/*
* COLORS
*/
// red corona
//colors[255/2] = new double[] {255, 0, 0};
// blue - white - red - black
colors[0] = new double[] {0, 0, 100};
colors[30] = new double[] {255, 255, 255};
colors[130] = new double[] {255, 100, 100};
colors[254] = new double[] {255, 255, 255};
// green - white - red - black
//colors[0] = new double[] {0, 100, 0};
//colors[10] = new double[] {255, 255, 255};
//colors[100] = new double[] {255, 0, 0};
// interpolate
if (colors[0] == null) colors[0] = new double[] {0, 0, 0};
if (colors[colors.length-1] == null) colors[colors.length-1] = new double[] {0, 0, 0};
for(int n = 0; n < colors.length; n++) {
if (colors[n] == null) {
int k = n;
while (colors[k] == null) {k++;}
double delta = k-n+1;
double deltar = (colors[k][0]-colors[n-1][0])/delta;
double deltag = (colors[k][1]-colors[n-1][1])/delta;
double deltab = (colors[k][2]-colors[n-1][2])/delta;
for (int m = n; m < k; m++) {
colors[m] = new double[] {colors[n-1][0]+deltar*(m-n+1), colors[n-1][1]+deltag*(m-n+1), colors[n-1][2]+deltab*(m-n+1)};
}
}
}
// color -> rgb
int[] intcolors = new int[colors.length];
for (int i = 0; i < colors.length; i++) {
Color c = new Color((int) colors[i][0], (int) colors[i][1], (int) colors[i][2]);
intcolors[i] = c.getRGB();
}
return intcolors;
}
}
Pingback: Third Anniversary – J-Blog