summaryrefslogtreecommitdiff
path: root/nl/mandel.nl
blob: 2bdc25bf00b5ff8cd93e10d1ac04dba8ba2d91f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
type int = i32;
type char = i8;
type string = ptr(char);

extern func void(int) putchar;
extern func u64(string) strlen;

void printnum(int n) {
	if (n < 0) {
		putchar('-');
		n = -n;
	}
	if (n == 0) {
		putchar('0');
		return;
	}
	while (n > 0) {
		putchar('0' + n % 10);
		n = n / 10;
	}
}

int maxiter;
double lbound;
double rbound;
double tbound;
double bbound;
double hincr;
double vincr;

int mandeliter(double x, double y) {
	double a = x;
	double b = y;
	double a2 = a * a;
	double b2 = b * b;
	int n = 0;
	while (n < maxiter && a2 + b2 < 4) {
		b = 2 * a * b + y;
		a = a2 - b2 + x;
		a2 = a * a;
		b2 = b * b;
		n = n + 1;
	}
	return n;
}

int main() {
	maxiter = 32;
	lbound = -2.0;
	rbound = 1.0;
	tbound = 1.5;
	bbound = -1.5;
	// hincr = 0.03125;
	hincr = 0.0625;
	vincr = 0.0625;

	string shade = " .,-:!%@#";
	int shadelen = cast(int)(strlen(shade));

	double y = tbound;
	while (y >= bbound) {
		double x = lbound;
		while (x <= rbound) {
			int niter = mandeliter(x, y);
			putchar(*(shade + niter*(shadelen-1)/maxiter));
			// printnum(niter);
			putchar(' ');
			x = x + hincr;
		}
		putchar('\n');
		y = y - vincr;
	}
	return 0;
}