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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# require "complex"
require "mandel"
require "tkclass"
DefaultMaxDepth = 30
DefaultSX = -2.25
DefaultSY = 1.75
DefaultEX = 1.25
DefaultEY = -1.75
def reset
$max_depth = DefaultMaxDepth
$s_re = DefaultSX
$s_im = DefaultSY
$e_re = DefaultEX
$e_im = DefaultEY
$dx = ($e_re - $s_re).abs / Width
$dy = ($e_im - $s_im).abs / Height
$photo.blank
end
Width = 400
Height = 400
$c = Canvas.new {
width Width
height Height
}
$c.pack
$c_rect = Rectangle.new($c, 0, 0, Width+1, Height+1)
$c_rect.fill "white"
$colors = []
def colors_init
$colors = []
for i in 0 .. 125
$colors.push(format("#%02x%02x%02x", 250 - (i*2), i*2, 0))
end
for i in 0 .. 125
$colors.push(format("#%02x%02x%02x", 0, 250 - (i*2), i*2))
end
$color_max = $colors.size - 1
end
def zoom(a, b, c, d)
center_x = (a + c) / 2
center_y = (b + d) / 2
size = (c - a).abs
size = (d - b).abs if (size < (d - b).abs)
size = 1 if (size < 1)
zoom_rate = ((Width + Height) / 2).to_f / size
$max_depth = ($max_depth.to_f * Math.sqrt(Math.sqrt(Math.sqrt(zoom_rate)))).to_i
move_x_rate = (center_x - (Width / 2)).to_f / (Width / 2)
move_y_rate = (center_y - (Height / 2)).to_f / (Height / 2)
center_re = ($s_re + $e_re) / 2
center_im = ($s_im + $e_im) / 2
c_size_re = ($e_re - $s_re).abs
c_size_im = ($e_im - $s_im).abs
center_re = center_re + (move_x_rate * (c_size_re / 2))
center_im = center_im - (move_y_rate * (c_size_im / 2))
$s_re = center_re - ((c_size_re / 2) / zoom_rate)
$s_im = center_im + ((c_size_im / 2) / zoom_rate)
$e_re = center_re + ((c_size_re / 2) / zoom_rate)
$e_im = center_im - ((c_size_im / 2) / zoom_rate)
$dx = ($e_re - $s_re).abs / Width
$dy = ($e_im - $s_im).abs / Height
p [$s_re, $dx, $s_im, $dy]
end
def mandel(x, y)
re = $s_re + ($dx * x)
im = $s_im - ($dy * y)
# z = c = Complex(re, im)
# for i in 0 .. $max_depth
# z = (z * z) + c
# break if z.abs > 2
# end
# return i
return Mandel.mandel(re, im, $max_depth)
end
$buf = "{"+" "*Width+"}"
def calc
$c.update
return if $current_rect
depth = 0
for x in 0 .. Width - 1
depth = mandel(x, $calc_y)
if depth >= $max_depth
$buf[x*8+1,7] = "#000000"
else
$buf[x*8+1,7] = $colors[$color_max * depth / $max_depth]
end
end
$photo.put($buf, 0, $calc_y)
$calc_y += 1
if (($calc_y % 20) == 0)
print "#{($calc_y * 100 / Height)}% done. -- depth #{$max_depth}\n"
# $mandel.image $photo
end
if ($calc_y > Height - 1)
$calc_y = StartCalcY
$calc_on = false
# exit
end
if $calc_on
Tk.after(1) { calc() }
end
end
$photo = TkPhotoImage.new({'width'=>Width, 'height'=>Height})
$mandel = TkcImage.new($c, Width/2, Height/2) { image $photo }
reset()
colors_init()
$calc_y = StartCalcY = 0
$calc_on = true
calc()
def clear
# $mandel.destroy if $mandel
$calc_y = StartCalcY
end
$start_x = $start_y = 0
$current_rect = nil
def do_press(x, y)
$start_x = x
$start_y = y
$current_rect = Rectangle.new($c, x, y, x, y) { outline "white" }
end
def do_motion(x, y)
if $current_rect
$current_rect.coords $start_x, $start_y, x, y
end
end
def do_release(x, y)
if $current_rect
$current_rect.coords $start_x, $start_y, x, y
$current_rect.destroy
$current_rect = nil
clear()
$calc_on = true
zoom($start_x, $start_y, x, y)
calc()
end
end
$c.bind("1", proc{|e| do_press e.x, e.y})
$c.bind("B1-Motion", proc{|x, y| do_motion x, y}, "%x %y")
$c.bind("ButtonRelease-1", proc{|x, y| do_release x, y}, "%x %y")
begin
Tk.mainloop
ensure
# File.delete("#tmpmandel#.gif")
end
|