For the 2 dimensional representation of fractals, I use a grid with a 1000×1000 resolution and a gradient colorization based on iteration/steps which also offsets the points on the Z-axis for depth.
Basic Mandelbrot set
To plot a Mandelbrot set, we can create a function in a point wrangle. Several attributes can be turned into a channel for more convenience. E.g the MaxIteration and scaling attribute.
As the colors are created in greyscale, it’s easy to convert it to other colors or gradients via a colornode.
Mandelbrot Code
//Mandelbrot function
function int Mandelbrot(float x1, y1; int maxIter)
{
float x0 = x1;
float y0 = y1;
for(int i=0; i<maxIter; i++)
{
float x2 = x0*x0 - y0*y0 + x1;
float y2 = 2*x0*y0 + y1;
if(x2*x2 + y2*y2 > maxIter) return(i);
x0 = x2;
y0 = y2;
}
return(maxIter);
}
//--- Main ---
v@Cd = 0; //set color to black
int maxiter = 30; //maximum iterations ~ resolution of the Mandelbrot set
int result = Mandelbrot(@P.x, @P.y, maxiter); //Main execution
//visualization
if(result < maxiter)
{
float step = 1-(1/float(maxiter)*result); //step calculation for each iteration
v@Cd = set(step,step,step); //color gradient
v@P.z = (v@P.z+0.1)*step; //Z-point offset (optional) the 0.1 is a scaling factor
}
Julia set
The Julia set(s) are part of the Mandelbrot set and can easily achieved by replacing the x1 and y1 in the for loop of our main function with a fixed value (see examples below). For the animation below, I replaced those values with a formula based on the current Framenumber.
Misc
Sierpinski triangle
Pretty simple to create as a loop iterating over a single triangle. Each iteration can be either done as a split or inverted inset. The center triangle is then deleted before the next iteration starts.
Sierpinski Code
int pts[] = primpoints(0, @primnum);
for(int i=0; i<len(pts); i++)
{
int pt = pts[i];
vector pos = point(0, "P", pt);
int triprim = addprim(0,"poly");
for(int n=0; n<len(pts); n++)
{
int npt = pts[n];
vector npos = point(0, "P", npt);
npos -= pos;
npos *= 0.5;
npos += pos;
int newpoint = addpoint(0, npos);
addvertex(0, triprim, newpoint);
}
}
removeprim(0, @primnum, 1);
2D Fractal Tree
Created as a line which splits in 3 branches in each iteration. Additionally with a channel parameter to control the angle.
At an angle of 90 degree, we get a squared shape, while 120 results in a sierpinski triangle type tree.
2D Fractal Tree Code
float steplenx = chf('steplenx');
float stepleny = chf('stepleny');
float stepangle = chf('stepangle');
vector pos = @P;
pos = pos + v@dir * steplenx;
matrix mat = ident();
rotate(mat, radians(stepangle), set(0,1,0));
pos *= mat;
pos += set(0,1,0) * stepleny;
int newpoint = addpoint(0, pos);
setpointattrib(0, "dir", newpoint, v@dir);
setpointgroup(0, "last", newpoint, 1);
setpointgroup(0, "last", @ptnum, 0);
3D Fractal Tree
Adding a third dimension to the 2D tree and additional parameters for control of several aspects.
Adding a color and polywire node at the output of the 3D fractal tree to colorize the lines and add volume.