-
Notifications
You must be signed in to change notification settings - Fork 1
/
cal_oc.sh
executable file
·61 lines (49 loc) · 2.09 KB
/
cal_oc.sh
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
# **************************************************************************** #
# #
# ::: :::::::: #
# cal_oc.sh :+: :+: :+: #
# +:+ +:+ +:+ #
# By: hyeonsok <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/02/04 01:40:54 by hyeonsok #+# #+# #
# Updated: 2022/02/04 05:31:18 by hyeonsok ### ########.fr #
# #
# **************************************************************************** #
#!/bin/bash
## [[ string =~ regex ]]
# The re-match operator performs regular expression matching of the string on
# its left to the right. If the left side matches the right side, the operator
# returns a 0 and a 1 otherwise.
read -p "look from x,y,z: " -a lookfrom
## Check the length of the array
if [[ $((${#lookfrom[@]})) -ne 3 || $((${#lookat[@]})) -ne 3 ]]; then
printf "Error\n"; exit 1
fi
read -p "look at x,y,z: " -a lookat
## Check the format of a floating-point number
for i in "${lookfrom[@]}"
do
if [[ $i =~ [:digit:]|([:digit:]\.[:digit:]) ]]; then
printf "Error\n"; exit 1
fi
done
for i in "${lookat[@]}"
do
if [[ $i =~ [:digit:]|([:digit:]\.[:digit:]) ]]; then
printf "Error\n"; exit 1
fi
done
## Find relative postion
x=$(echo "${lookat[0]} - ${lookfrom[0]}" | bc -l)
y=$(echo "${lookat[1]} - ${lookfrom[1]}" | bc -l)
z=$(echo "${lookat[2]} - ${lookfrom[2]}" | bc -l)
## Square calculation
xx=$(echo $x \* $x | bc -l)
yy=$(echo $y \* $y | bc -l)
zz=$(echo $z \* $z | bc -l)
distance=$(echo "sqrt($xx + $yy + $zz)" | bc -l)
## Vector normalization
vec_i=$(echo "$x / $distance" | bc -l)
vec_j=$(echo "$y / $distance" | bc -l)
vec_k=$(echo "$z / $distance" | bc -l)
printf "%.5f,%.5f,%.5f\n" $vec_i $vec_j $vec_k