Shouldn't it be rad2deg and not deg2rad? Also with atan the return value is in the range [-pi/2, pi/2]. Maybe use atan2 which the return value is in the range [-pi, pi], but you'll need to change to return [0,360). Start with an easy test like 0, 45, 180, 270 and 315 to see if you got it right. Test it!
#!/usr/bin/perl
use Math::Trig;
$XH = 1.0;
$YH = 7.2;
$XT = 0.0;
$YT = 6.2;
$angle = atan2(($YT - $YH) , ($XT - $XH));
#$heading = deg2rad($angle);
$heading = rad2deg($angle);
$heading += 360 if ( $heading < 0 );
printf ("heading approx. %.2f degrees\n", $heading);
$XH = 1.0;
$YH = 7.2;
$XT = 2.0;
$YT = 8.2;
$angle = atan2(($YT - $YH) , ($XT - $XH));
#$heading = deg2rad($angle);
$heading = rad2deg($angle);
$heading += 360 if ( $heading < 0 );
printf ("heading approx. %.2f degrees\n", $heading);
$XH = 756979.0;
$YH = 7951269.2;
$XT = 765484.9;
$YT = 7951229.6;
$angle = atan2(($YT - $YH) , ($XT - $XH));
#$heading = deg2rad($angle);
$heading = rad2deg($angle);
$heading += 360 if ( $heading < 0 );
printf ("heading approx. %.2f degrees\n", $heading); Output:
heading approx. 225.00 degrees
heading approx. 45.00 degrees
heading approx. 359.73 degrees histrungalot
Posting Whiz in Training
266 posts since May 2008
Reputation Points: 76
Solved Threads: 34