Skip to content

Commit 1239719

Browse files
committed
Add mandelbrot code
1 parent b3ef2bf commit 1239719

File tree

10 files changed

+1044
-0
lines changed

10 files changed

+1044
-0
lines changed

mandelbrot/.gitignore

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
*.bin
2+
3+
# Prerequisites
4+
*.d
5+
6+
# Object files
7+
*.o
8+
*.ko
9+
*.obj
10+
*.elf
11+
12+
# Linker output
13+
*.ilk
14+
*.map
15+
*.exp
16+
17+
# Precompiled Headers
18+
*.gch
19+
*.pch
20+
21+
# Libraries
22+
*.lib
23+
*.a
24+
*.la
25+
*.lo
26+
27+
# Shared objects (inc. Windows DLLs)
28+
*.dll
29+
*.so
30+
*.so.*
31+
*.dylib
32+
33+
# Executables
34+
*.exe
35+
*.out
36+
*.app
37+
*.i*86
38+
*.x86_64
39+
*.hex
40+
41+
# Debug files
42+
*.dSYM/
43+
*.su
44+
*.idb
45+
*.pdb
46+
47+
# Kernel Module Compile Results
48+
*.mod*
49+
*.cmd
50+
.tmp_versions/
51+
modules.order
52+
Module.symvers
53+
Mkfile.old
54+
dkms.conf

mandelbrot/LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

mandelbrot/README.md

Lines changed: 110 additions & 0 deletions
Large diffs are not rendered by default.

mandelbrot/color.png

42.4 KB
Loading

mandelbrot/compile.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/sh
2+
COMPILER="${COMPILER:-tcc}"
3+
4+
# $1 - input .c file
5+
# -o - Output filename
6+
# -W - Turn on various types of warnings
7+
8+
set -x
9+
if [ "$COMPILER" = "gcc" ]; then
10+
# -Oz - Optimize for code size
11+
# -g - Debug symbols
12+
# -fsanitize... - Show lots of nice colorful info when stack smashing is detected
13+
gcc "${1}" -o "${1%.c}.bin" -Wall -Wpedantic -Oz -g -fsanitize=address
14+
else
15+
tcc "${1}" -o "${1%.c}.bin" -Wall -Wpedantic -Oz -g
16+
fi

mandelbrot/golf2.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#define S 3321
2+
int main(i, x, y, t) {
3+
// Size is width=81, height=41, total is S=3321
4+
float r, m, R[S] = {0}, M[S] = {0}, C[S], D[S];
5+
6+
// y is imaginary, x is real
7+
for (i = 0; i < S; i++) {
8+
// The boundaries go from x=real=-2 to 0.5, y=imag=-1 to 1
9+
// x = i % width
10+
C[i] = 2.5 * (i % 81) / 80. - 2;
11+
// y = i / width
12+
D[i] = 2 * (i / 81) / 40. - 1;
13+
}
14+
15+
t = 0;
16+
while (t++ < 1063) {
17+
for (i = 0; i < S; i++) {
18+
// Compute previous_latice**2 + c
19+
// c**2 = (r + im)**2 = r**2 - m**2 + 2irm
20+
r = R[i];
21+
m = M[i];
22+
R[i] = r * r - m * m + C[i];
23+
M[i] = 2 * r * m + D[i];
24+
}
25+
}
26+
27+
for (i = 0; i < S; i++) {
28+
// Compute the magnitude of this point
29+
// |c|**2 = r**2 + m**2
30+
// If the magnitude is less than 4, the point did not diverge
31+
printf(R[i] * R[i] + M[i] * M[i] < 4 ? "█" : "▒");
32+
i % 81 == 80 && puts("");
33+
}
34+
}

mandelbrot/golf2_minified.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define S 3321
2+
main(i,x,y,t){float r,m,R[S]={0},M[S]={0},C[S],D[S];for(i=0;i<S;i++){C[i]=2.5*(i%81)/80.-2;D[i]=2*(i/81)/40.-1;}t=0;while(t++<1063){for(i=0;i<S;i++){r=R[i];m=M[i];R[i]=r*r-m*m+C[i];M[i]=2*r*m+D[i];}}for(i=0;i<S;i++){printf(R[i]*R[i]+M[i]*M[i]<4?"█":"▒");i%81==80&&puts("");}}

mandelbrot/img/color261.png

190 KB
Loading

mandelbrot/reference.c

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
typedef float num;
5+
6+
num magnitude(num real, num imag) {
7+
return real * real + imag * imag;
8+
}
9+
10+
num squared_re(num real, num imag) {
11+
return real * real - imag * imag;
12+
}
13+
14+
num squared_im(num real, num imag) {
15+
return 2 * real * imag;
16+
}
17+
18+
typedef struct args_t {
19+
size_t width;
20+
size_t height;
21+
size_t max_iterations;
22+
size_t color;
23+
num min_re;
24+
num max_re;
25+
num min_im;
26+
num max_im;
27+
} args_t;
28+
29+
#define AE "\033["
30+
const char* BCOLORS[16] = {
31+
AE "40m", AE "41m", AE "42m", AE "43m", AE "44m", AE "45m", AE "46m", AE "47m", AE "100m", AE "101m", AE "102m", AE "103m", AE "104m", AE "105m", AE "106m", AE "107m"
32+
};
33+
const char* FCOLORS[16] = {
34+
AE "30m", AE "31m", AE "32m", AE "33m", AE "34m", AE "35m", AE "36m", AE "37m", AE "90m", AE "91m", AE "92m", AE "93m", AE "94m", AE "95m", AE "96m", AE "97m"
35+
};
36+
const char* CLEAR_COLOR = AE "0m";
37+
38+
void mandelbrot(args_t args) {
39+
size_t size = args.width * args.height;
40+
num* lattice_re = calloc(size, sizeof(num));
41+
num* lattice_im = calloc(size, sizeof(num));
42+
num* c_re = calloc(size, sizeof(num));
43+
num* c_im = calloc(size, sizeof(num));
44+
// An array of unsigned ints
45+
size_t* iter_to_diverge = calloc(size, sizeof(size_t));
46+
47+
// y is imaginary, x is real
48+
for (size_t y = 0; y < args.height; y++) {
49+
for (size_t x = 0; x < args.width; x++) {
50+
c_re[x + y * args.width] = (args.max_re - args.min_re) * x / (args.width - 1) + args.min_re;
51+
c_im[x + y * args.width] = (args.max_im - args.min_im) * y / (args.height - 1) + args.min_im;
52+
}
53+
}
54+
55+
size_t iteration = 0;
56+
while (iteration < args.max_iterations) {
57+
for (size_t i = 0; i < size; i++) {
58+
num new_re = squared_re(lattice_re[i], lattice_im[i]) + c_re[i];
59+
num new_im = squared_im(lattice_re[i], lattice_im[i]) + c_im[i];
60+
lattice_re[i] = new_re;
61+
lattice_im[i] = new_im;
62+
if (magnitude(new_re, new_im) >= 2 * 2 && iter_to_diverge[i] == 0) {
63+
iter_to_diverge[i] = iteration;
64+
}
65+
}
66+
iteration++;
67+
}
68+
69+
for (size_t y = 0; y < args.height; y++) {
70+
for (size_t x = 0; x < args.width; x++) {
71+
num r = lattice_re[x + y * args.width];
72+
num m = lattice_im[x + y * args.width];
73+
// Test if the value at the given point diverged
74+
if (magnitude(r, m) < 2 * 2) {
75+
if (args.color) {
76+
// TODO: render higher resolution with braille chars
77+
printf("%s█%s", FCOLORS[0], CLEAR_COLOR);
78+
} else {
79+
printf("█");
80+
}
81+
} else {
82+
if (args.color) {
83+
size_t i = iter_to_diverge[x + y * args.width] % 16;
84+
// printf("%s%lx%s", BCOLORS[i], i, CLEAR_COLOR);
85+
printf("%s%s %s", FCOLORS[(i + 2) % 16], BCOLORS[i], CLEAR_COLOR);
86+
} else {
87+
printf("▒");
88+
}
89+
}
90+
//printf("%10.5f+%10.5fi |c|=%10.5f\n", r, m, magnitude(r, m));
91+
}
92+
puts("");
93+
}
94+
95+
free(lattice_re);
96+
free(lattice_im);
97+
free(c_re);
98+
free(c_im);
99+
free(iter_to_diverge);
100+
}
101+
102+
int main(int argc, char* argv[]) {
103+
args_t args = {
104+
.width = 81,
105+
.height = 41,
106+
.max_iterations = 1063,
107+
.color = 1,
108+
.min_re = -2,
109+
.max_re = 0.5,
110+
.min_im = -1,
111+
.max_im = 1,
112+
// .min_re = -2 / 2,
113+
// .max_re = 0.5 / 2,
114+
// .min_im = -1,
115+
// .max_im = 0,
116+
};
117+
if (argc >= 3) args.width = strtol(argv[1], NULL, 0);
118+
if (argc >= 3) args.height = strtol(argv[2], NULL, 0);
119+
if (argc >= 4) args.max_iterations = strtol(argv[3], NULL, 0);
120+
mandelbrot(args);
121+
return 0;
122+
}

mandelbrot/tile.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python3
2+
import argparse
3+
4+
parser = argparse.ArgumentParser()
5+
parser.add_argument("--map-fname", "-m", required=True)
6+
parser.add_argument("--src-fname", "-s", required=True)
7+
parser.add_argument("--replace", "-r", default="▒")
8+
parser.add_argument("--new-char", "-w", type=str, default=None)
9+
parser.add_argument("--step", type=int, default=1)
10+
11+
def main():
12+
args = parser.parse_args()
13+
with open(args.map_fname) as mfile, open(args.src_fname) as sfile:
14+
mapcontents = mfile.read()
15+
source = sfile.read().replace('\n', '\\n')
16+
out = []
17+
i = 0
18+
replace_chars = 0
19+
for char in mapcontents:
20+
if char in args.replace:
21+
if replace_chars % args.step == 0 and i < len(source):
22+
out.append(source[i])
23+
i += 1
24+
else:
25+
out.append(args.new_char or char)
26+
replace_chars += 1
27+
else:
28+
out.append(char)
29+
print(''.join(out))
30+
31+
if __name__ == "__main__":
32+
main()

0 commit comments

Comments
 (0)