2 declare __asteroids_grid_height
3 declare __asteroids_grid_width
6 echo $__asteroids_grid_height
10 echo $__asteroids_grid_width
16 if [ ! -e "$filename" ]; then
17 echo "Couldn't find file: $filename"
23 declare -g -A asteroids
25 __asteroids_grid_height=0
26 __asteroids_grid_width=0
28 while read -u 3 line; do
29 __asteroids_grid_width=${#line}
30 for (( a=0; a<${#line}; a++ )); do
32 if [ "${char}" == "#" ]; then
33 asteroids[$a,$__asteroids_grid_height]=0
36 __asteroids_grid_height=$((__asteroids_grid_height+1))
49 echo "$x_diff,$y_diff"
52 get_asteroids_vector() {
61 get_vector $x1 $y1 $x2 $y2
68 local vector=$(get_asteroids_vector $a1 $a2)
69 local move_x=${vector%,*}
70 local move_y=${vector#*,}
72 val="$(calc -p 'round(((90 + ((180/pi()) * atan2('$move_y','$move_x'))) % 360) * 100000000) / 100000000')"
76 get_closest_asteroid() {
81 local nearest_asteroid=
85 for asteroid in $asteroids; do
86 vector=$(get_asteroids_vector $our_asteroid $asteroid)
89 # use absolute numbers for length calculation!
94 if [ $distance -lt $min_dist -o $min_dist -eq -1 ]; then
96 nearest_asteroid=$asteroid
100 echo $nearest_asteroid
103 remove_asteroid_from_string() {
112 for a in $asteroids; do
113 if [ "$a" != "$asteroid" ]; then
114 new_asteroids+="${seperator}$a"
116 if [ "x$new_asteroids" != "x" ]; then
121 echo "$new_asteroids"
125 local -n __asteroid_angles=$1
126 printf "%s\n" "${!__asteroid_angles[@]}" | sort -g