I have been trying unsuccessfully to calculate the "bearing" between 2 Universal Transverse Mercator points. Lets call them head and tail.
head has points $XH = 756979.0 $YH = 7951269.2 (x,y)
tail has point $XT = 765484.9 $YT = 7951229.6 (x,y)
what I have been trying is as follows: (variables already defined...and chomped)
use Math::Trig;
$angle = atan(($YT - $YH) / ($XT - $XH));
$heading = deg2rad($angle);
printf ("heading approx. %.2f degrees\n", $heading);
But I am getting nonsense numbers back from this. I did a fair bit of googleing and installed Math::Trig, which provides the atan function and deg2rad function. atan returns the angle in radians, and the deg2rad I thought returned the heading between 2 points in degrees. Anyone tried this before and got it to work?
I also have tried the following, stolen from another person's script:
$angle = atan2($YL - $YF, $XL - $XF);
$angledeg = (180/3.1415926)*$angle;
if ( $angledeg < -90 ) {
$headingdeg = abs((180/3.1415926)*$angle) + 90;
}
elsif ( $angledeg < 0 ) {
$headingdeg = abs((180/3.1415926)*$angle) + 180;
}
elsif ( $angledeg < 90 ) {
$headingdeg = 90 - (180/3.1415926)*$angle;
}
else {
$headingdeg = 180 - (180/3.1415926)*$angle;
}
printf ("Line heading approx. %.2f degrees\n", $headingdeg);
but I am still getting the quadrant wrong. Its doing my head in.