use warnings; use strict; use Math::Trig; use constant PI => 3.14159265358979; # calgary my $lat1 = 51.32055555555556; my $lon1 = -114.7297222222222221; # toronto my $lat2 = 43.65321111111111; my $lon2 = -79.38321111111111; my $bearing = bearing($lat1, $lon1, $lat2, $lon2); my $direction = direction($bearing); print "$bearing, $direction\n"; sub bearing { my ($lat1, $lon1, $lat2, $lon2) = map { ($_ * PI) / 180 } @_; my $d_lon = $lon2 - $lon1; my $y = sin($d_lon) * cos($lat2); my $x = cos($lat1) * sin($lat2) - sin($lat1) * cos($lat2) * cos($d_lon); my $rad = atan2($y, $x); my $deg = $rad * (180 / PI); return $deg < 0 ? $deg += 360 : $deg; } sub direction { my ($deg) = @_; my @directions = qw( N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW N ); my $calc = (($deg % 360) / 22.5) + .5; return $directions[$calc]; } __END__ 94.0048844699519, E