Created
May 7, 2018 14:56
-
-
Save George3d6/2da6d6266846299d31ca04c904a6abe5 to your computer and use it in GitHub Desktop.
Notes for a blog covering an introduction to julia using julia sets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Pkg.add("Plots") | |
#Pkg.add("GR") | |
using Plots | |
@everywhere function pixel_color(x, y, width, height, c) | |
max_iter = 255 | |
z = ( ((y/width)*2.7 - 1.3) + ((x/height)*4.5 - 2.5) *im ) | |
for iter_nr = 1:max_iter | |
z = z^2 + c | |
if abs(z) >= 4 | |
return iter_nr | |
end | |
end | |
return -255 | |
end | |
@everywhere function julia_set(height, width, c) | |
jset = Array{Int32}(height, width) | |
println("Generating set !") | |
for x = 1:height | |
for y = 1:width | |
jset[x, y] = pixel_color(x, y, width, height, c) | |
end | |
end | |
jset | |
end | |
function run() | |
height = 1380 | |
width = 1380 | |
#c = 0.7885e ^ (0.8im) | |
#data = julia_set(height, width, c) | |
#hm = heatmap(data, size=(width,height), color=:ice) | |
#png("/tmp/julia_$(height)_$(width)") | |
nr_frames = 300 | |
sets = @sync @parallel append! for n=0:nr_frames | |
c = e ^ (2*π*n/nr_frames*im) | |
set = julia_set(height, width, c) | |
[set] | |
end | |
anim = @animate for set in sets | |
heatmap(set, size=(width,height), color=:inferno, leg=false) | |
end | |
gif(anim, "/tmp/julia_$(height)_$(width)_inferno2.gif", fps=15) | |
end | |
function mandelbrot_set() | |
height = 2000 | |
width = 2000 | |
max = 300 | |
mset = Array{Int32}(height, width) | |
println("Generating set !") | |
for x = 1:height | |
for y = 1:width | |
c = ( (y - width/2) * (4/width) + ((x - height/2) * (4/width))*im ) | |
z = (0 + 0im) | |
current_iter = nothing | |
for iter = 1:max | |
current_iter = iter | |
z = z^2 + c | |
if abs(z) > 4 | |
break | |
end | |
end | |
mset[x, y] = current_iter | |
end | |
end | |
println("Ploting !") | |
heatmap(mset, size=(width,height)) | |
png("/tmp/mandelbrot_$(height)_$(width)_$(max)") | |
end | |
function mandelbrot_set_para() | |
height = 5000 | |
width = 5000 | |
max = 255 | |
println("Generating set !") | |
mset = @sync @parallel (hcat) for x = 1:height | |
col = Array{Int32}(width) | |
for y = 1:width | |
c = ( (y - width/2) * (4/width) + ((x - height/2) * (4/width))*im ) | |
z = (0 + 0im) | |
current_iter = nothing | |
for iter = 1:max | |
current_iter = iter | |
z = z^2 + c | |
if abs(z) > 4 | |
break | |
end | |
end | |
col[y] = current_iter | |
end | |
col | |
end | |
println("Ploting !") | |
heatmap(mset, size=(width,height)) | |
png("/tmp/mandelbrot") | |
end | |
function lorenz_attractor() | |
# initialize the attractor | |
n = 300 | |
dt = 0.02 | |
σ, ρ, β = 10., 28., 8/3 | |
x, y, z = 1., 1., 1. | |
# initialize a 3D plot with 1 empty series | |
plt = path3d(1, xlim=(-25,25), ylim=(-25,25), zlim=(0,50), | |
xlab = "x", ylab = "y", zlab = "z", | |
title = "Lorenz Attractor", marker = 1, | |
linecolor=RGBA(162/255,72/255,72/255,1/1)) | |
# build an animated gif, saving every 10th frame | |
gui() | |
@gif for i=1:n | |
dx = σ*(y - x) | |
x += dt * dx | |
dy = x*(ρ - z) - y | |
y += dt * dy | |
dz = x*y - β*z | |
z += dt * dz | |
push!(plt, x, y, z) | |
end every 10 | |
end | |
function layer(x0, y0, z0, n) | |
x,y,z = -0.3,0.2,0.11 | |
for i in 1:46 | |
r = sqrt(x^2 + y^2 + z^2) | |
φ = atan2(y, x) | |
θ = atan2(sqrt(x^2 + y^2), z) | |
x = r^n * sin(n*θ)*cos(n*φ) + x0 | |
y = r^n * sin(n*θ)*sin(n*φ) + y0 | |
z = r^n * cos(n*θ) +z0 | |
if sum([x^2,y^2,z^2]) >= 2 | |
#println("$x0 $y0 $z0 $i") | |
return 259 - i * 4 | |
end | |
end | |
return 0 | |
end | |
function try_3d() | |
xmax, ymax, zmax = 280, 280, 140 | |
colors = Array{RGBA}(xmax*ymax*zmax) | |
X = Array{Int32}(xmax*ymax*zmax) | |
Y = Array{Int32}(xmax*ymax*zmax) | |
Z = Array{Int32}(xmax*ymax*zmax) | |
i = 0 | |
for x in 1:xmax | |
for y in 1:ymax | |
for z in 1:zmax | |
i += 1 | |
color = layer((1.8*x/xmax) - 0.9, (1.8*y/ymax) - 0.9, z/zmax, 8) | |
transparency = 60 | |
if color >= 255 | |
transparency = 0 | |
elseif color > 200 | |
transparency = 2 * ( 255/color ) | |
end | |
R = color | |
B = color | |
G = color | |
if color < 255 && color > 120 | |
B = B/5 | |
elseif color > 60 && color <= 120 | |
R = R/2 | |
B = B/2 | |
G = G*1.2 | |
if G > 250 | |
G = 250 | |
end | |
elseif color <= 60 | |
R = (R + 4)*10 | |
if R > 250 | |
R = 250 | |
end | |
B = B/2 | |
G = G/2 | |
end | |
colors[i] = RGBA(R/255, G/255, B/255, transparency/255) | |
X[i] = x | |
Y[i] = y | |
Z[i] = z | |
end | |
end | |
end | |
println("Plotting !") | |
scatter3d(X,Y,Z,leg=false,cbar=true,w=5, c=colors, m=(1,0.0,colors,stroke(0))) | |
png("/tmp/nest2") | |
end | |
#pyplot() | |
#run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment