aboutsummaryrefslogtreecommitdiffstats
path: root/examples/graphstyles/density.jl
blob: 957b48719836135f2fdca653e70f7841db5ad612 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

# Original Python:
#   from pyx import *
#
#   # Mandelbrot calculation contributed by Stephen Phillips
#
#   # Mandelbrot parameters
#   re_min = -2
#   re_max = 0.5
#   im_min = -1.25
#   im_max = 1.25
#   gridx = 100
#   gridy = 100
#   max_iter = 10
#
#   # Set-up
#   re_step = (re_max - re_min) / gridx
#   im_step = (im_max - im_min) / gridy
#   d = []
#
#   # Compute fractal
#   for re_index in range(gridx):
#       re = re_min + re_step * (re_index + 0.5)
#       for im_index in range(gridy):
#           im = im_min + im_step * (im_index + 0.5)
#           c = complex(re, im)
#           n = 0
#           z = complex(0, 0)
#           while n < max_iter and abs(z) < 2:
#               z = (z * z) + c
#               n += 1
#           d.append([re, im, n])
#
#   # Plot graph
#   g = graph.graphxy(height=8, width=8,
#                   x=graph.axis.linear(min=re_min, max=re_max, title=r"$\Re(c)$"),
#                   y=graph.axis.linear(min=im_min, max=im_max, title=r'$\Im(c)$'))
#   g.plot(graph.data.points(d, x=1, y=2, color=3, title="iterations"),
#       [graph.style.density(gradient=color.rgbgradient.Rainbow)])
#   g.writeEPSfile()
#   g.writePDFfile()
#   g.writeSVGfile()

using PyX
using LaTeXStrings

# Mandelbrot parameters
re_min = -2
re_max = 0.5
im_min = -1.25
im_max = 1.25
gridx = 100
gridy = 100
max_iter = 10

# Set-up
re_step = (re_max - re_min) / gridx
im_step = (im_max - im_min) / gridy
# Julia note: indexing is opposite of what would be idiomatic Julia
d = zeros(gridx*gridy, 3)

# Compute fractal
for re_index in 0:(gridx-1)
    re = re_min + re_step * (re_index + 0.5)
    for im_index in 0:(gridy-1)
        im = im_min + im_step * (im_index + 0.5)
        c = complex(re, im)
        n = 0
        z = complex(0, 0)
        while n < max_iter && abs(z) < 2
            z = (z * z) + c
            n += 1
        end
        d[re_index*gridy + im_index + 1, :] = [re, im, n]
    end
end

# Plot graph
g = graph.graphxy(height=8, width=8,
                  x=graph_axis.linear(min=re_min, max=re_max, title=L"$\Re(c)$"),
                  y=graph_axis.linear(min=im_min, max=im_max, title=L"$\Im(c)$"))
plot(g, graph_data.points(d, x=1, y=2, color=3, title="iterations"),
        [graph_style.density(gradient=color_rgbgradient.Rainbow)])
writeEPSfile(g, "density")
writePDFfile(g, "density")