var blob = use('blob')

var iter = 50
var limit = 2.0
var zr = null
var zi = null
var cr = null
var ci = null
var tr = null
var ti = null
var y = 0
var x = 0
var i = 0
var row = null

var h = Number(arg[0]) || 500
var w = h

log.console(`P4\n${w} ${h}`);

for (y = 0; y < h; ++y) {
  row = blob(w);

  for (x = 0; x < w; ++x) {
    zr = 0; zi = 0; tr = 0; ti = 0;
    cr = 2 * x / w - 1.5;
    ci = 2 * y / h - 1;
    for (i = 0; i < iter && (tr + ti <= limit * limit); ++i) {
      zi = 2 * zr * zi + ci;
      zr = tr - ti + cr;
      tr = zr * zr;
      ti = zi * zi;
    }

    if (tr + ti <= limit * limit)
      row.write_bit(1);
    else
      row.write_bit(0);
  }

  stone(row)

  log.console(text(row, 'b'));
}

$stop()