mandelbrot_like_set.jl 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/julia
  2. # Trizen
  3. # 17 January 2017
  4. # https://github.com/trizen
  5. # Generates a Mandelbrot-like set.
  6. # See also:
  7. # https://en.wikipedia.org/wiki/Mandelbrot_set
  8. # https://trizenx.blogspot.com/2017/01/mandelbrot-set.html
  9. using Images
  10. @inline function hsv2rgb(h, s, v)
  11. c = v * s
  12. x = c * (1 - abs(((h/60) % 2) - 1))
  13. m = v - c
  14. if h < 60
  15. r,g,b = (c, x, 0)
  16. elseif h < 120
  17. r,g,b = (x, c, 0)
  18. elseif h < 180
  19. r,g,b = (0, c, x)
  20. elseif h < 240
  21. r,g,b = (0, x, c)
  22. elseif h < 300
  23. r,g,b = (x, 0, c)
  24. else
  25. r,g,b = (c, 0, x)
  26. end
  27. (r + m), (b + m), (g + m)
  28. end
  29. #
  30. ## A specific function which maps the value of `c` to some other value.
  31. #
  32. @inline function f(c)
  33. 1/c
  34. end
  35. function mandelbrot()
  36. w, h = 1000, 1000
  37. zoom = 1 # the zoom factor
  38. moveX = 0 # the amount of shift on the x axis
  39. moveY = 0 # the amount of shift on the y axis
  40. L = 100 # the maximum value of |z|
  41. I = 30 # the maximum number of iterations
  42. img = zeros(RGB{Float64}, h, w)
  43. for x in 1:w, y in 1:h
  44. c = Complex(
  45. (2*x - w) / (w * zoom) + moveX,
  46. (2*y - h) / (h * zoom) + moveY
  47. )
  48. z = c
  49. n = 0
  50. q = f(c)
  51. while (abs(z) < L && (n += 1) < I)
  52. z = z^q
  53. end
  54. v = (I - n) / I
  55. r,g,b = hsv2rgb(v*360, 1, v)
  56. img[y,x] = RGB{Float64}(r, g, b)
  57. end
  58. println("Generating image...")
  59. save("mandelbrot_like_set.png", img)
  60. end
  61. mandelbrot()