forked from fetchai/etch-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelbrot.etch
85 lines (84 loc) · 2.76 KB
/
mandelbrot.etch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// TWS, April 28th, Mandelbrot Set (simple):
function main()
//
printLn("Mandelbrot Set");
//
// The "screen" size and the screen itself (double height for mirrored set):
var xMax : Int32 = 100;
var yMax : Int32 = 32;
var halfOffset : Int32 = yMax * xMax;
var screen = Array<String>(xMax * (yMax * 2));
var maxIterations : Int32 = 230;
var maxSprites : Float64 = 32.0;
//
// Draw the mandelbrot set:
for (pixelY in 0:yMax - 1)
for (pixelX in 0:xMax - 1)
//
// Scale X to mandlebrot scale (x -2.5 to 1):
var xLocal : Float64 = toFloat64(pixelX);
xLocal = ((xLocal / toFloat64(xMax)) * 3.5) - 2.5;
//
// Now Y to -1 to 1:
var yLocal : Float64 = toFloat64(pixelY);
yLocal = yLocal / toFloat64(yMax);
//
// Now let's do the algorithm bit:
var x : Float64 = 0.0;
var y : Float64 = 0.0;
var iteration : Int32 = 0;
while (((x * x) + (y * y)) <= 4.0 && iteration < maxIterations)
var xTemp = (x * x) - (y * y) + xLocal;
y = 2.0 * x * y + yLocal;
x = xTemp;
iteration++;
endwhile
//
// Render based on iteration achieved:
var colourSlide : Float64 = (toFloat64(iteration) / toFloat64(maxIterations));
var colourIndex : Int32 = toInt32(colourSlide * maxSprites);
//
// Pick a character according to colourIndex (iterations achieved):
var insertCharacter : String = " ";
if (colourIndex < 2)
insertCharacter = " ";
elseif (colourIndex < 6)
insertCharacter = ".";
elseif (colourIndex < 10)
insertCharacter = "'";
elseif (colourIndex < 20)
insertCharacter = "+";
else
insertCharacter = "*";
endif
//
// Bottom half of mandlebrot set:
var bIndex : Int32 = halfOffset + (pixelY * xMax) + pixelX;
screen[bIndex] = insertCharacter;
//
// Top mirror half:
var inverseY = yMax - pixelY;
var tIndex : Int32 = (inverseY * xMax) + pixelX;
screen[tIndex] = insertCharacter;
endfor
endfor
//
// Render the buffer out:
for (y in 0:(yMax * 2) - 1)
var line : String = "";
for (x in 0:xMax - 1)
//
// Build the line, deal with my own stupidity with the screen array
// by testing for NULL:
var index : Int32 = (y * xMax) + x;
if (screen[index] == null)
line = line + " ";
else
line = line + screen[index];
endif
endfor
//
// Output this line and proceed to next:
printLn(line);
endfor
endfunction