Reputation: 373
We got homework to convert Cartesian Coordinates to Polar Coordinates and I don't even know how to start. So any little help would be nice.
We have to write code in bash. Here is the example that we got:
script.sh 256 128
Result: 286.216 1.107
We have to insert number as parameters and we have to use bc interpreter.
Upvotes: 2
Views: 812
Reputation: 2935
Because it's your homework, i wrote your program but i put hook into it.My task has some advantages for you:
I only example for call function, unless i complete my program, you see at end of my prog, some call func, it's not my entire main.write a main for it.
My program:
#!/bin/bash
### X and Y
export x=$1
export y=$2
####calculating r:
export r=`echo "sqrt(x*x+y*y)" |bc -l `
####Radian to Degree
r2d(){
echo "180,3.14156265358979323846,`echo $1`" |awk ' BEGIN {FS=","} { print $1/$2*$3'} ###HOOK
}
####Degree to Radian
d2r(){
echo "180,3.14156265358979323846,`echo $1`" |awk ' BEGIN {FS=","} { print $2/$1*$3'} ###HOOK
}
teta(){
if [ $x -lt 0 ];then
echo `echo "$1,$2" | awk 'BEGIN {FS=","} {print 3.14156265358979323846+atan2($2/$1,1)'}` ####HOOK
fi;
if [ $x -gt 0 ];then
echo `echo "$1,$2" | awk 'BEGIN {FS=","} {print atan2($2/$1,1)'}` ####HOOK
fi;
}
echo `r2d 2.09`
echo `d2r 120`
echo `teta $x,$y`
Upvotes: 1
Reputation: 247042
Some tips:
-l
option to bcsqrt()
and a()
functionsscale=3
to limit the precisionbc
reads its program from stdinUpvotes: 1