From 3a4147a37d4d547985f72236e2f8c7db7c9fcf03 Mon Sep 17 00:00:00 2001 From: dtrg Date: Sat, 28 Apr 2007 22:34:05 +0000 Subject: [PATCH] Added the Mandelbrot generator. --- examples/.distr | 1 + examples/README | 2 ++ examples/mandelbrot.c | 73 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 examples/mandelbrot.c diff --git a/examples/.distr b/examples/.distr index 8aaa2deab..3469c16ff 100644 --- a/examples/.distr +++ b/examples/.distr @@ -3,6 +3,7 @@ hilo.c hilo.mod hilo.ocm hilo.p +mandelbrot.c paranoia.c startrek.c startrek.doc diff --git a/examples/README b/examples/README index 6e5081886..b77720d95 100644 --- a/examples/README +++ b/examples/README @@ -17,8 +17,10 @@ hilo.mod Modula-2 version of the Hi/Lo game hilo.ocm Occam 1 version of the Hi/Lo game hilo.p Pascal version of the Hi/Lo game +mandelbrot.c A simple Mandelbrot generator, using floating-point maths paranoia.c An ancient public domain K&R C adventure game startrek.c An even more ancient public domain ANSI C game + (uses the FPU for distance calculation) startrek.doc Manual for above (plus in current directory when running) Enjoy. diff --git a/examples/mandelbrot.c b/examples/mandelbrot.c new file mode 100644 index 000000000..27484ee90 --- /dev/null +++ b/examples/mandelbrot.c @@ -0,0 +1,73 @@ +/* $Source$ + * $State$ + * $Revision$ + */ + +/* Adapted from code by Chris Losinger. (This is the non-obfuscated + * version... + * + * http://www.codeproject.com/cpp/mandelbrot_obfuscation.asp + */ + +#include +#include + +enum +{ + ROWS = 40, + COLUMNS = 60, + + MAX_ITERATIONS = 255 +}; + +void nl(void) +{ + write(1, "\n", 1); +} + +void out(int n) +{ + const char* chars = "****++++++---- "; + write(1, chars + (n/16), 1); +} + +int main(int argc, const char* argv[]) +{ + /* Size and position of the visible area. */ + + double view_r = -2.3, view_i = -1.0; + double zoom = 0.05; + int x, y, n; + + for (y=0; y < ROWS; y++) + { + double c_i = view_i + y * zoom; + + for (x=0; x < COLUMNS; x++) + { + double c_r = view_r + x*zoom; + double z_r = c_r; + double z_i = c_i; + + for (n=0; n < MAX_ITERATIONS; n++) + { + double z_r2 = z_r * z_r; + double z_i2 = z_i * z_i; + + /* Have we escaped? */ + + if (z_r2 + z_i2 > 4) + break; + + /* z = z^2 + c */ + z_i = 2 * z_r * z_i + c_i; + z_r = z_r2 - z_i2 + c_r; + } + + out(n); + } + nl(); + } + + return 0; +}