From eebbc9cab74692325b64482d8ac0a92389bee61b Mon Sep 17 00:00:00 2001 From: Alex Hirsch Date: Sun, 3 Mar 2019 12:01:55 +0100 Subject: [PATCH] Examples: Mandelbrot reads num iterations from stdin --- examples/mandelbrot/mandelbrot.mc | 11 +++++++---- examples/mandelbrot/mandelbrot.stdin.txt | 1 + 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/examples/mandelbrot/mandelbrot.mc b/examples/mandelbrot/mandelbrot.mc index b279b89..46ebe16 100644 --- a/examples/mandelbrot/mandelbrot.mc +++ b/examples/mandelbrot/mandelbrot.mc @@ -12,7 +12,7 @@ float transform_y(float y) return ((1.0) - ((1.0) - (-1.0)) * (fy / 50.0)) * 2.0; } -bool is_in_set(float pX, float pY) +bool is_in_set(float pX, float pY, int iterations) { float temp; float x; @@ -23,14 +23,14 @@ bool is_in_set(float pX, float pY) y = 0.0; i = 0; - while (((x * x + y * y) <= 4.0) && i < 30) { + while (((x * x + y * y) <= 4.0) && i < iterations) { temp = x * x - y * y + pX; y = 2.0 * x * y + pY; x = temp; i = i + 1; } - if (i == 30) + if (i == iterations) return true; return false; @@ -45,6 +45,9 @@ int main() y = 0.0; + int iterations; + iterations = read_int(); + while (y < 50.0) { x = 0.0; @@ -53,7 +56,7 @@ int main() fy = transform_y(y); x = x + 1.0; - if (is_in_set(fx, fy)) { + if (is_in_set(fx, fy, iterations)) { print("."); } else { print(" "); diff --git a/examples/mandelbrot/mandelbrot.stdin.txt b/examples/mandelbrot/mandelbrot.stdin.txt index e69de29..64bb6b7 100644 --- a/examples/mandelbrot/mandelbrot.stdin.txt +++ b/examples/mandelbrot/mandelbrot.stdin.txt @@ -0,0 +1 @@ +30