Skip to content

Commit 5529a22

Browse files
author
caral
committed
add mandelbrot ascii visualizer
1 parent e740f7c commit 5529a22

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

mandelbrot-ascii-c/mandelbrot.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <stdio.h>
2+
#include <sys/ioctl.h>
3+
#include <unistd.h>
4+
5+
#define MIN_X -2.5
6+
#define MAX_X 1.5
7+
8+
#define MIN_Y -1.5
9+
#define MAX_Y 1.5
10+
11+
#define ITER 100
12+
13+
int get_window_size(int *rows, int *cols) {
14+
struct winsize w;
15+
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) {
16+
return -1; // Error getting terminal size
17+
}
18+
*rows = w.ws_col;
19+
*cols = w.ws_row;
20+
return 0;
21+
}
22+
23+
double map(double n, double start1, double stop1, double start2, double stop2) {
24+
return (n - start1) / (stop1 - start1) * (stop2 - start2) + start2;
25+
}
26+
27+
int main() {
28+
int width, height;
29+
if (get_window_size(&width, &height) != 0) {
30+
printf("Failed to get terminal size\n");
31+
return 1;
32+
}
33+
for (double row = 0; row < height; row++) {
34+
double y = map(row, 0, height, MIN_Y, MAX_Y);
35+
for (double col = 0; col < width; col++) {
36+
double x = map(col, 0, width, MIN_X, MAX_X);
37+
38+
double c_real = x;
39+
double c_imag = y;
40+
41+
double z_real = 0;
42+
double z_imag = 0;
43+
44+
int iter;
45+
for (iter = 0; iter < ITER; iter++) {
46+
double z_real_new = z_real * z_real - z_imag * z_imag + c_real;
47+
double z_imag_new = 2 * z_real * z_imag + c_imag;
48+
49+
z_real = z_real_new;
50+
z_imag = z_imag_new;
51+
52+
double magnitude_squared = z_real * z_real + z_imag * z_imag;
53+
if (magnitude_squared > 4) {
54+
break;
55+
}
56+
}
57+
58+
if (iter == ITER) {
59+
putchar('*'); // Point is in the set
60+
} else if (iter > ITER / 2) {
61+
putchar('-');
62+
} else if (iter > ITER / 3) {
63+
putchar('+');
64+
} else {
65+
putchar(' '); // Point diverged
66+
}
67+
}
68+
putchar('\n');
69+
}
70+
71+
return 0;
72+
}

mandelbrot-ascii-c/readme.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# ASCII Mandelbrot (C)
2+
3+
This program renders a low-resolution ASCII visualization of the Mandelbrot Set directly in the terminal.
4+
It adapts to the terminal windows dimensions.
5+
6+
### How to Run
7+
```bash
8+
gcc mandelbrot.c -o mandelbrot
9+
./mandelbrot
10+
```
11+
12+
### Example Output
13+
```bash
14+
15+
16+
*
17+
-
18+
+++ +**- +
19+
********
20+
+********
21+
++ **-+***********-+* *-+ +
22+
-***+************************ +***-
23+
+ +********************************-
24+
+**************************************
25+
+ ++ ---****************************************
26+
*++ +*+ - +******************************************
27+
+************- ********************************************+
28+
-****************-+*******************************************+
29+
*-*++*************************************************************
30+
**************************************************************************************+
31+
*-*++*************************************************************
32+
-****************-+*******************************************+
33+
+************- ********************************************+
34+
*++ +*+ - +******************************************
35+
+ ++ ---****************************************
36+
+**************************************
37+
+ +********************************-
38+
-***+************************ +***-
39+
++ **-+***********-+* *-+ +
40+
+********
41+
********
42+
+++ +**- +
43+
-
44+
*
45+
46+
47+
```

0 commit comments

Comments
 (0)