Mandelbrot Set
Mathematically, the Mandelbrot set is defined on the plane of complex numbers
1
2
//Function
3
def mandelbrot(wd,ht,mx)
4
{
5
return [Imperative]
6
{
7
it = [];
8
for (rw in 0..ht-1)
9
{
10
for (cl in 0..wd-1)
11
{
12
rl = (cl - wd/2.0)*4.0/wd;
13
im = (rw - ht/2.0)*4.0/wd;
14
x = 0;
15
y = 0;
16
n = 0;
17
while (x*x+y*y <= 4 && n < mx)
18
{
19
x1 = x*x - y*y + rl;
20
y = 2*x*y + im;
21
x = x1;
22
n = n + 1;
23
}
24
it[cl][rw] = n;
25
}
26
}
27
return it;
28
}
29
};
Copied!
1
wd = 200;
2
hg = 200;
3
m = mandelbrot(wd,hg,50);
4
5
//Visualization
6
q = List.Reverse(List.Sort(List.UniqueItems(List.Flatten(m,-1))));
7
r = List.DropItems(Math.Round(Math.RemapRange(q,10,250)),-1);
8
d = Dictionary.ByKeysValues(q+"", List.Flatten([Color.ByARGB
9
(250,175,230,125),Color.ByARGB(List.Reverse(r),List.ShiftIndices
10
(r,100),List.ShiftIndices(r,-13),List.Reverse(r))],-1));
11
c = Image.FromPixels(List.Transpose(d.ValueAtKey(m+"")));
12
13
//Surface
14
pt1 = Point.ByCoordinates((-wd/2..wd/2..#wd)<1>,(-hg/2..hg/2..#hg)<2>);
15
el1 = List.Chop(Math.RemapRange(List.Flatten(m,-1),0,hg/10),hg);
16
sr1 = NurbsSurface.ByControlPoints(pt1.Translate(Vector.ZAxis(),el1));
17
sr2 = GeometryColor.BySurfaceColors(sr1,d.ValueAtKey(m+""));
Copied!
Maximum Iterations: 10
Maximum Iterations: 30
Joni "Let’s draw the Mandelbrot set!", November 17, 2013 <https://jonisalonen.com/2013/lets-draw-the-mandelbrot-set/> January 26, 2021
Last modified 1mo ago
Copy link