forked from yaneurao/Pytra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_julia_parameter_sweep.rb
More file actions
95 lines (89 loc) · 2.68 KB
/
06_julia_parameter_sweep.rb
File metadata and controls
95 lines (89 loc) · 2.68 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
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
require_relative "py_runtime"
# 06: Sample that sweeps Julia-set parameters and outputs a GIF.
def julia_palette()
palette = __pytra_bytearray(256 * 3)
__pytra_set_index(palette, 0, 0)
__pytra_set_index(palette, 1, 0)
__pytra_set_index(palette, 2, 0)
i = 1
while i < 256
t = ((__pytra_float(i - 1)) / 254.0)
r = __pytra_int((255.0 * ((((9.0 * (1.0 - t)) * t) * t) * t)))
g = __pytra_int((255.0 * ((((15.0 * (1.0 - t)) * (1.0 - t)) * t) * t)))
b = __pytra_int((255.0 * ((((8.5 * (1.0 - t)) * (1.0 - t)) * (1.0 - t)) * t)))
__pytra_set_index(palette, (i * 3 + 0), r)
__pytra_set_index(palette, (i * 3 + 1), g)
__pytra_set_index(palette, (i * 3 + 2), b)
i += 1
end
return __pytra_bytes(palette)
end
def render_frame(width, height, cr, ci, max_iter, phase)
frame = __pytra_bytearray(width * height)
__hoisted_cast_1 = __pytra_float(height - 1)
__hoisted_cast_2 = __pytra_float(width - 1)
y = 0
while y < height
row_base = y * width
zy0 = ((-1.2) + (2.4 * __pytra_div(y, __hoisted_cast_1)))
x = 0
while x < width
zx = ((-1.8) + (3.6 * __pytra_div(x, __hoisted_cast_2)))
zy = zy0
i = 0
while i < max_iter
zx2 = zx * zx
zy2 = zy * zy
if zx2 + zy2 > 4.0
break
end
zy = ((2.0 * zx * zy) + ci)
zx = (zx2 - zy2 + cr)
i += 1
end
if i >= max_iter
__pytra_set_index(frame, row_base + x, 0)
else
color_index = (1 + (((i * 224 / max_iter) + phase) % 255))
__pytra_set_index(frame, row_base + x, color_index)
end
x += 1
end
y += 1
end
return __pytra_bytes(frame)
end
def run_06_julia_parameter_sweep()
width = 320
height = 240
frames_n = 72
max_iter = 180
out_path = "sample/out/06_julia_parameter_sweep.gif"
start = __pytra_perf_counter()
frames = []
center_cr = (-0.745)
center_ci = 0.186
radius_cr = 0.12
radius_ci = 0.1
start_offset = 20
phase_offset = 180
__hoisted_cast_3 = __pytra_float(frames_n)
i = 0
while i < frames_n
t = __pytra_div(((i + start_offset) % frames_n), __hoisted_cast_3)
angle = (2.0 * Math::PI * t)
cr = (center_cr + radius_cr * Math.cos(__pytra_float(angle)))
ci = (center_ci + radius_ci * Math.sin(__pytra_float(angle)))
phase = ((phase_offset + i * 5) % 255)
frames.append(render_frame(width, height, cr, ci, max_iter, phase))
i += 1
end
save_gif(out_path, width, height, frames, julia_palette(), 8, 0)
elapsed = __pytra_perf_counter() - start
__pytra_print("output:", out_path)
__pytra_print("frames:", frames_n)
__pytra_print("elapsed_sec:", elapsed)
end
if __FILE__ == $PROGRAM_NAME
run_06_julia_parameter_sweep()
end