From 5529a2212cd51f0930756a7518d9cc1475688d9b Mon Sep 17 00:00:00 2001 From: caral Date: Sun, 8 Jun 2025 16:14:30 +0200 Subject: [PATCH 1/2] add mandelbrot ascii visualizer --- mandelbrot-ascii-c/mandelbrot.c | 72 +++++++++++++++++++++++++++++++++ mandelbrot-ascii-c/readme.md | 47 +++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 mandelbrot-ascii-c/mandelbrot.c create mode 100644 mandelbrot-ascii-c/readme.md diff --git a/mandelbrot-ascii-c/mandelbrot.c b/mandelbrot-ascii-c/mandelbrot.c new file mode 100644 index 00000000..5cae27af --- /dev/null +++ b/mandelbrot-ascii-c/mandelbrot.c @@ -0,0 +1,72 @@ +#include +#include +#include + +#define MIN_X -2.5 +#define MAX_X 1.5 + +#define MIN_Y -1.5 +#define MAX_Y 1.5 + +#define ITER 100 + +int get_window_size(int *rows, int *cols) { + struct winsize w; + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) { + return -1; // Error getting terminal size + } + *rows = w.ws_col; + *cols = w.ws_row; + return 0; +} + +double map(double n, double start1, double stop1, double start2, double stop2) { + return (n - start1) / (stop1 - start1) * (stop2 - start2) + start2; +} + +int main() { + int width, height; + if (get_window_size(&width, &height) != 0) { + printf("Failed to get terminal size\n"); + return 1; + } + for (double row = 0; row < height; row++) { + double y = map(row, 0, height, MIN_Y, MAX_Y); + for (double col = 0; col < width; col++) { + double x = map(col, 0, width, MIN_X, MAX_X); + + double c_real = x; + double c_imag = y; + + double z_real = 0; + double z_imag = 0; + + int iter; + for (iter = 0; iter < ITER; iter++) { + double z_real_new = z_real * z_real - z_imag * z_imag + c_real; + double z_imag_new = 2 * z_real * z_imag + c_imag; + + z_real = z_real_new; + z_imag = z_imag_new; + + double magnitude_squared = z_real * z_real + z_imag * z_imag; + if (magnitude_squared > 4) { + break; + } + } + + if (iter == ITER) { + putchar('*'); // Point is in the set + } else if (iter > ITER / 2) { + putchar('-'); + } else if (iter > ITER / 3) { + putchar('+'); + } else { + putchar(' '); // Point diverged + } + } + putchar('\n'); + } + + return 0; +} diff --git a/mandelbrot-ascii-c/readme.md b/mandelbrot-ascii-c/readme.md new file mode 100644 index 00000000..051017ff --- /dev/null +++ b/mandelbrot-ascii-c/readme.md @@ -0,0 +1,47 @@ +# ASCII Mandelbrot (C) + +This program renders a low-resolution ASCII visualization of the Mandelbrot Set directly in the terminal. +It adapts to the terminal windows dimensions. + +### How to Run +```bash +gcc mandelbrot.c -o mandelbrot +./mandelbrot +``` + +### Example Output +```bash + + + * + - + +++ +**- + + ******** + +******** + ++ **-+***********-+* *-+ + + -***+************************ +***- + + +********************************- + +************************************** + + ++ ---**************************************** + *++ +*+ - +****************************************** + +************- ********************************************+ + -****************-+*******************************************+ + *-*++************************************************************* + **************************************************************************************+ + *-*++************************************************************* + -****************-+*******************************************+ + +************- ********************************************+ + *++ +*+ - +****************************************** + + ++ ---**************************************** + +************************************** + + +********************************- + -***+************************ +***- + ++ **-+***********-+* *-+ + + +******** + ******** + +++ +**- + + - + * + + +``` From 698112f1164dd18c7db4ffb2120004d35a38b354 Mon Sep 17 00:00:00 2001 From: caral Date: Sun, 8 Jun 2025 16:19:28 +0200 Subject: [PATCH 2/2] fix readme --- mandelbrot-ascii-c/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mandelbrot-ascii-c/readme.md b/mandelbrot-ascii-c/readme.md index 051017ff..847acd91 100644 --- a/mandelbrot-ascii-c/readme.md +++ b/mandelbrot-ascii-c/readme.md @@ -10,7 +10,7 @@ gcc mandelbrot.c -o mandelbrot ``` ### Example Output -```bash +```txt *