#!/usr/bin/ruby

#
## https://rosettacode.org/wiki/Kronecker_product_based_fractals
#

func kronecker_product (a, b) { [a,b].cartesian.map_2d {|a,b| a ~X* b } }

func kronecker_fractal(pattern, order=4) {
    var kronecker = pattern
    { kronecker = kronecker_product(kronecker, pattern) } * order
    return kronecker
}

var order = 5
var shape = [[1, 1, 1], [0, 1, 0], [0, 1, 0]]

require("Imager")

var pat = kronecker_fractal(shape, order)
var img = %O<Imager>.new(xsize => pat[0].len, ysize => pat.len)

var c1 = %n<255 255 32>
var c2 = %n<16 16 16>

for y in (^pat.len), x in (^pat[0].len) {
    img.setpixel(x => x, y => y, color => (pat[y][x] ? c1 : c2))
}


img.write(file => "kronecker_fractal.png")
