Skip to content

Instantly share code, notes, and snippets.

Created February 15, 2018 18:59
Show Gist options
  • Save anonymous/bdc06bab7802a02893808f1a63a46550 to your computer and use it in GitHub Desktop.
Save anonymous/bdc06bab7802a02893808f1a63a46550 to your computer and use it in GitHub Desktop.
import complex, sequtils
import threadpool
{.experimental.}
proc mandel(x, y: float, maxIters: int): int =
var
c = (re: x, im: y)
z = (re: 0.0, im: 0.0)
for i in 0 ..< maxIters:
z = z*z + c
if (z.re*z.re + z.im*z.im) >= 4:
return i
return 255
proc fractalRow(min_x, pxSizeX, imag: float): array[16000, int] =
for x in 0 ..< 16000:
let real = min_x + float(x) * pxSizeX
result[x] = mandel(real, imag, 20)
proc createFractal(min_x, max_x, min_y, max_y: float, iters: int): array[12000, array[16000, int]] =
parallel:
let
height = result.len
width = result[0].len
pxSizeX = (max_x - min_x) / float(width)
pxSizeY = (max_y - min_y) / float(height)
for y in 0 ..< height:
let imag = min_y + float(y) * pxSizeY
result[y] = spawn fractalRow(min_x, pxSizeX, imag)
let r = createFractal(-2.0, 1.0, -1.0, 1.0, 20)
echo r[51][72]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment