Julia set
require('Imager')
var (w, h) = (640, 480)
var img = %s'Imager'.new(xsize => w, ysize => h, channels => 3)
var maxIter = 50
var c = Complex(-0.388, 0.613)
var color = %s'Imager::Color'.new('#000000')
for x,y in (^w ~X ^h) {
var i = maxIter
var z = Complex((x - w/2) / w * 3, (y - h/2) / h * 2)
while (z.abs < 2 && --i) {
z = (z*z + c)
}
color.set(hsv => [i / maxIter * 360, 1, i])
img.setpixel(x => x, y => y, color => color)
}
img.write(file => "JuliaSet_sidef.png")
This version generates an ASCII representation:
var (w, h) = (141, 50)
var maxIter = 40
var c = Complex(-0.8, 0.156)
for y in ^h {
for x in ^w {
var i = maxIter
var z = Complex(3 * (x - w/2) / w, 2 * (y - h/2) / h)
while (z.abs < 2 && --i) {
z = (z*z + c)
}
print (i > 0 ? ' ' : '#')
}
print "\n"
}