-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjulia_set.jl
More file actions
68 lines (53 loc) · 1.4 KB
/
julia_set.jl
File metadata and controls
68 lines (53 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/julia
# Trizen
# 27 March 2016
# https://github.com/trizen
# Generate a random Julia set as a PNG image.
# See also:
# https://en.wikipedia.org/wiki/Julia_set
using Images
@inline function hsv2rgb(h, s, v)
c = v * s
x = c * (1 - abs(((h/60) % 2) - 1))
m = v - c
if h < 60
r,g,b = (c, x, 0)
elseif h < 120
r,g,b = (x, c, 0)
elseif h < 180
r,g,b = (0, c, x)
elseif h < 240
r,g,b = (0, x, c)
elseif h < 300
r,g,b = (x, 0, c)
else
r,g,b = (c, 0, x)
end
(r + m), (b + m), (g + m)
end
function julia_set()
w, h = 1000, 1000
zoom = 0.5 # the zoom factor
moveX = 0 # the amount of shift on the x axis
moveY = 0 # the amount of shift on the y axis
L = 2 # the maximum value of |z|
I = 30 # the maximum number of iterations
img = zeros(RGB{Float64}, h, w)
c = Complex(-rand(), 2 * rand() * (rand() < 0.5 ? 1 : -1))
for x in 1:w, y in 1:h
n = 0
z = Complex(
(2*x - w) / (w * zoom) + moveX,
(2*y - h) / (h * zoom) + moveY
)
while abs(z) < L && (n += 1) < I
z = z^2 + c
end
v = (I - n) / I
r,g,b = hsv2rgb(v*360, 1, v)
img[y,x] = RGB{Float64}(r, g, b)
end
println("Generating image...")
save("$c-$zoom.png", img)
end
julia_set()