summaryrefslogtreecommitdiff
path: root/nl
diff options
context:
space:
mode:
Diffstat (limited to 'nl')
-rw-r--r--nl/mandel.nl69
1 files changed, 69 insertions, 0 deletions
diff --git a/nl/mandel.nl b/nl/mandel.nl
new file mode 100644
index 0000000..cef9eb2
--- /dev/null
+++ b/nl/mandel.nl
@@ -0,0 +1,69 @@
+type int = i32;
+type char = i8;
+type string = ptr(char);
+
+extern func void(int) putchar;
+
+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;
+
+ double y = tbound;
+ while (y >= bbound) {
+ double x = lbound;
+ while (x <= rbound) {
+ int niter = mandeliter(x, y);
+ printnum(niter);
+ putchar(' ');
+ x = x + hincr;
+ }
+ putchar('\n');
+ y = y - vincr;
+ }
+ return 0;
+}