diff --git a/examples/mandelbrot/mandelbrot.q b/examples/mandelbrot/mandelbrot.q new file mode 100644 index 0000000..4f29761 --- /dev/null +++ b/examples/mandelbrot/mandelbrot.q @@ -0,0 +1,47 @@ +import io + +main() { + line := new(byte, 80) + + loop row := 0..40 { + loop col := 0..80 { + cx := -160000 + col * 1250 + cy := -50000 + row * 2500 + line[col] = glyph(mandel(cx, cy)) + } + + io.writeLine(line) + } +} + +mandel(cx int, cy int) -> int { + x := cx + y := cy + + loop i := 0..400 { + nx := (x * x - y * y) / 100000 + cx + ny := (x * y * 2) / 100000 + cy + x = nx + y = ny + + if x * x + y * y > 40000000000 { + return i + } + } + + return -1 +} + +glyph(n int) -> byte { + if n < 0 { + return '#' + } + + bucket := n / 20 + + if bucket > 9 { + bucket = 9 + } + + return " .:-=+*#%@"[bucket] +} \ No newline at end of file diff --git a/tests/examples_test.go b/tests/examples_test.go index 70f5d60..d0ff841 100644 --- a/tests/examples_test.go +++ b/tests/examples_test.go @@ -21,6 +21,7 @@ var examples = []run{ {"echo", nil, "Echo", "Echo", 0}, {"fnl", []string{"-remove"}, "Hello\n", "Hello", 0}, {"clock", nil, "", "", -1}, + {"mandelbrot", nil, "", "", -1}, {"raylib", nil, "", "", -1}, {"readfile", nil, "", "", -1}, {"server", nil, "", "", -1},