Previous: Error Dispatching Samples, Up: Samples [Index]
# Write a Mandelbrot PGM image, square, of specified size, on standard output.
# byte buffer
data b;
file f;
integer w, h;
integer i, iter, x, y;
real Zr, Zi, Cr, Ci, Tr, Ti;
f_open(f, "/dev/stdout", OPEN_WRITEONLY, 0);
# N is (to be) provided externally, it is the size of the image
w = N;
h = N;
# resize the byte buffer
b_size(b, N);
iter = 50;
# write the PGM header
f_plan(f, "P5\n", N, " ", N, "\n", 255, "\n");
y = 0;
while (y < h) {
Ci = (2r * y / h - 1);
x = 0;
while (x < w) {
Zr = 0;
Zi = 0;
Tr = 0;
Ti = 0;
Cr = 2r * x / w;
Cr -= 1.5;
i = iter;
while (i && Tr + Ti < 4) {
Zi *= 2;
Zi = Zr * Zi + Ci;
Zr = Tr - Ti + Cr;
Tr = Zr * Zr;
Ti = Zi * Zi;
i -= 1;
}
# set pixel in line, sort of:
# b[x] = ?
if (4 < Tr + Ti) {
b_replace(b, x, 255);
} else {
b_replace(b, x, 0);
}
x += 1;
}
# write line
f_data(f, b);
y += 1;
}
Run as:
aime FILE integer N 256 > fractal.pgm