-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelbrot.mar
48 lines (44 loc) · 1.31 KB
/
mandelbrot.mar
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
import ui.mar
fun main() {
var texture = screen_texture()
var zoom = 1.0
var move_x = 0.0 - 0.5
var move_y = 0.0
var max_iterations = 256
var width = texture.width.to_float()
var height = texture.height.to_float()
loop {
for x in 0..texture.width do
for y in 0..texture.height do {
var real = 1.5 * {x.to_float() - {width / 2.0}}
/ {0.5 * zoom * width} + move_x
var imag = {y.to_float() - {height / 2.0}}
/ {0.5 * zoom * height} + move_y
var value = mandelbrot(real, imag, max_iterations)
| println("Pixel at {x}, {y} looped {value} times")
var color = if value == max_iterations
then black
else color(value % 256, value % 256, value % 256)
texture.&.draw(x @ y, color)
}
texture.show()
zoom.& *= {11.to_float() / 10.to_float()}
}
}
fun mandelbrot(real: Float, imag: Float, max_iterations: Int): Int {
var new_real = 0.0
var new_imag = 0.0
var old_real = 0.0
var old_imag = 0.0
var i = 0
loop {
if i == max_iterations then break
i = i + 1
old_real = new_real
old_imag = new_imag
new_real = {old_real * old_real} - {old_imag * old_imag} + real
new_imag = 2.0 * old_real * old_imag + imag
if {new_real * new_real} + {new_imag * new_imag} > 4.0 then break
}
i
}