use strict;
use Math::Trig qw(deg2rad pi great_circle_distance asin acos);
############################
## Distances are in Miles ##
############################
my ($lat1, $long1);
my ($lat2, $long2);
print "Haversine : ". &Haversine($lat1, $long1, $lat2, $long2) ."\n"
+;
print "Law Cosines : ". &LawCosines($lat1, $long1, $lat2, $long2)."\n"
+;
print "Flat-Earth : ". &FlatEarth($lat1, $long1, $lat2, $long2) ."\n"
+;
print "Great Circle: ". &GreatCircle($lat1, $long1, $lat2, $long2)."\n
+";
sub LawCosines {
my ($lat1, $long1, $lat2, $long2) = @_;
my $r=3956;
my $dist = acos(sin(deg2rad($lat1))*
sin(deg2rad($lat2))+
cos(deg2rad($lat1))*
cos(deg2rad($lat2))*
cos(deg2rad($long2)- deg2rad($long1)))
* $r;
return $dist;
}
sub FlatEarth {
my ($lat1, $long1, $lat2, $long2) = @_;
my $r=3956;
my $a = (pi/2)- deg2rad($lat1);
my $b = (pi/2)- deg2rad($lat2);
my $c = sqrt($a**2 + $b**2 - 2 * $a *$b
*cos(deg2rad($long2)-deg2rad($long1)));
my $dist = $c * $r;
return $dist;
}
sub Haversine {
my ($lat1, $long1, $lat2, $long2) = @_;
my $r=3956;
$dlong = deg2rad($long1) - deg2rad($long2);
$dlat = deg2rad($lat1) - deg2rad($lat2);
$a = sin($dlat/2)**2 +cos(deg2rad($lat1))
* cos(deg2rad($lat2))
* sin($dlong/2)**2;
$c = 2 * (asin(sqrt($a)));
$dist = $r * $c;
return $dist;
}
sub GreatCircle {
my ($lat1, $long1, $lat2, $long2) = @_;
my $r=3956;
my @zip1 = (deg2rad($long1), deg2rad(90-$lat1));
my @zip2 = (deg2rad($long2), deg2rad(90-$lat2));
my $dist = great_circle_distance(@zip1, @zip2,$r);
return $dist;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.