diff options
| author | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-17 12:14:26 +0000 |
|---|---|---|
| committer | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-17 12:14:26 +0000 |
| commit | d15dbaf025df9265dce417faaa5ccf33ae04d4b5 (patch) | |
| tree | 57201674789a305c0bc7b82632fa906534dbf37d /examples | |
| parent | 91e763a600afc86e9630e4db592d8108f65def16 (diff) | |
Fix for #31
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/features/comptime_fib.zc (renamed from examples/comptime_fib.zc) | 0 | ||||
| -rw-r--r-- | examples/features/showcase.zc (renamed from examples/showcase.zc) | 0 | ||||
| -rw-r--r-- | examples/graphics/mandelbrot.zc | 78 | ||||
| -rw-r--r-- | examples/graphics/raylib_demo.zc (renamed from examples/raylib_demo.zc) | 0 |
4 files changed, 78 insertions, 0 deletions
diff --git a/examples/comptime_fib.zc b/examples/features/comptime_fib.zc index 1ad2898..1ad2898 100644 --- a/examples/comptime_fib.zc +++ b/examples/features/comptime_fib.zc diff --git a/examples/showcase.zc b/examples/features/showcase.zc index eca5480..eca5480 100644 --- a/examples/showcase.zc +++ b/examples/features/showcase.zc diff --git a/examples/graphics/mandelbrot.zc b/examples/graphics/mandelbrot.zc new file mode 100644 index 0000000..f3fa4bd --- /dev/null +++ b/examples/graphics/mandelbrot.zc @@ -0,0 +1,78 @@ + +struct Complex { + re: float, + im: float, +} + +fn complex_make(re: float, im: float) -> Complex { + return Complex { re: re, im: im }; +} + +fn complex_add(a: Complex, b: Complex) -> Complex { + return Complex { re: a.re + b.re, im: a.im + b.im }; +} + +fn complex_mul(a: Complex, b: Complex) -> Complex { + return Complex { + re: a.re * b.re - a.im * b.im, + im: a.re * b.im + a.im * b.re + }; +} + +fn complex_abs2(z: Complex) -> float { + return z.re * z.re + z.im * z.im; +} + +fn complex_print(z: Complex) { + println "{z.re}{z.im}i"; +} + +fn pick_char(iter: int, max_iter: int, edge_chars: char[], edge_count: int) -> char { + if (iter >= max_iter) { return ' '; } + if (iter <= 0) { return edge_chars[0]; } + + var t: float = ((float)iter) / ((float)max_iter); + var idx: int = (int)(t * ((float)(edge_count - 1))); + + if (idx < 0) { idx = 0; } + if (idx >= edge_count) { idx = edge_count - 1; } + + return edge_chars[idx]; +} + +fn main() { + var width: int = 120; + var height: int = 40; + var max_iter: int = 200; + + var edge_chars: char[12] = [ '#', '@', '%', '8', '&', '*', '+', '=', '-', ':', '.', ',' ]; + var edge_count: int = 12; + + var min_re: float = -2.2; + var max_re: float = 1.0; + var min_im: float = -1.2; + var max_im: float = 1.2; + + for y in 0..height { + var im: float = + max_im - (max_im - min_im) * ( ((float)y) / ((float)(height - 1)) ); + + for x in 0..width { + var re: float = + min_re + (max_re - min_re) * ( ((float)x) / ((float)(width - 1)) ); + + var c: Complex = complex_make(re, im); + var z: Complex = complex_make(0.0, 0.0); + + var iter: int = 0; + while (iter < max_iter and complex_abs2(z) <= 4.0) { + z = complex_add(complex_mul(z, z), c); + iter += 1; + } + + var pixel = pick_char(iter, max_iter, edge_chars, edge_count); + print "{pixel}"; + } + print "\n"; + } +} diff --git a/examples/raylib_demo.zc b/examples/graphics/raylib_demo.zc index 77b661e..77b661e 100644 --- a/examples/raylib_demo.zc +++ b/examples/graphics/raylib_demo.zc |
