-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandelbrot
executable file
·97 lines (85 loc) · 2.2 KB
/
mandelbrot
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
#!/usr/bin/env bash
# Copyright (C) 2018 Grzegorz Wcisło
main() {
init
while :; do
draw
# read one character of unbuffered input
read -rsn1 -d ''
case "${REPLY}" in
# use awk for floating point calculations
w) y=$(echo "$y $scale" | awk '{printf "%f", $1+3/$2}') ;;
s) y=$(echo "$y $scale" | awk '{printf "%f", $1-3/$2}') ;;
d) x=$(echo "$x $scale" | awk '{printf "%f", $1+6/$2}') ;;
a) x=$(echo "$x $scale" | awk '{printf "%f", $1-6/$2}') ;;
q) scale=$(echo "$scale" | awk '{printf "%f", $1/1.1}') ;;
e) scale=$(echo "$scale" | awk '{printf "%f", $1*1.1}') ;;
i) py=$(echo "$py" | awk '{printf "%f", $1+0.01}') ;;
k) py=$(echo "$py" | awk '{printf "%f", $1-0.01}') ;;
l) px=$(echo "$px" | awk '{printf "%f", $1+0.02}') ;;
j) px=$(echo "$px" | awk '{printf "%f", $1-0.02}') ;;
f) switch_fractal ;;
?) cleanup ;;
esac
done
}
init() {
resize
# set default parameters
x=-0.5
y=0
scale=$DEFAULT_SCALE
max_iter=100
fractal=m
# save terminal state, hide cursor and clear
tput smcup
tput civis
tput clear
trap 'resize; draw' WINCH # terminal resize signal
trap cleanup INT
}
resize() {
# set new width and height
w=$(tput cols)
h=$(tput lines)
DEFAULT_SCALE=$h
}
draw() {
# move cursor to (0,0) and draw fractal
tput cup 0 0
${BASH_SOURCE[0]}.out $fractal $w $h $scale $x $y $max_iter $px $py
show_debug
}
show_debug() {
tput cup 0 0
echo "Position: ($x,$y)"
echo "Zoom: $scale"
if [ $fractal = j ]; then
echo "Parameter: ($px,$py)"
fi
}
switch_fractal() {
if [ $fractal = m ]; then
# switch to Julia, set parameter values and save Mandelbrot scale
fractal=j
px=$x
py=$y
m_scale=$scale;
x=0
y=0
scale=$DEFAULT_SCALE
else
# switch to Mandelbrot, restore saved values
fractal=m
x=$px
y=$py
scale=$m_scale
fi
}
cleanup() {
# restore teminal state and show cursor
tput rmcup
tput cnorm
exit 0
}
main