Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
#!/usr/local/bin/perl -w use strict; use POSIX qw(atan acos floor); # # Use www.mapblast.com, or your coordinates from the MonkMap page if +you're already there. # Latitude and longtitude are expressed in decimal notation. North a +nd east values are # positive, South and west are negative. # { my @location_of_me = qw(34.172500 -84.001667); # latitude=34.17 +2500, longitude=-84.001667 of jcwren my @location_of_vroom = qw(42.763333 -86.110556); # latitude=42.76 +3333, longitude=-86.110556 of vroom print sprintf ("\nMy T-shirt has to travel %0.f miles, on a heading + of %.1f degrees, to get from vroom to me\n\n", range_and_bearing (@location_of_vroom, @location_of_ +me)); } # # Returns range (in miles), and angle (relative to $latitude1/$longti +tude1) # # Uses basic great circle route calculation. Output compares favorab +ly with www.mapblast.com # www.mapquest.com, and Delorme Street Atlas. Minor deviations since + the earth is not a true # sphere, but bulges slighty at the equator. Accurate enough for com +mon distance calculations, # do not use to calculate fuel usage for cruise missiles targetting ( +0.2% error seems to be # typical worst case, though) # sub range_and_bearing { my ($latitude1, $longitude1, $latitude2, $longitude2) = @_; my $pi = 3.1415926535897; my $radToDeg = 180.0 / $pi; my $degToRad = $pi / 180.0; my $earthRadius = 3958.9; # Use 3958.9=miles, 6371.0=K +m; my $distance = 0; my $azimuth = 0; my $beta = 0; my $cosBeta = 0; my $cosAzimuth = 0; $latitude1 = $latitude1 * $degToRad; $longitude1 = $longitude1 * $degToRad; $latitude2 = $latitude2 * $degToRad; $longitude2 = $longitude2 * $degToRad; if (abs ($latitude1) < 90.0) { $cosBeta = (sin ($latitude1) * sin ($latitude2)) + ((cos ($latit +ude1) * cos ($latitude2)) * cos ($longitude2 - $longitude1)); if ($cosBeta >= 1.0) { return (0.0, 0.0); } # # Antipodes (return miles, 0 degrees) # if ($cosBeta <= -1.0) { return (floor ($earthRadius * $pi * 100.0) / 100.0, 0.0); } $beta = acos ($cosBeta); $distance = $beta * $earthRadius; $cosAzimuth = (sin ($latitude2) - sin ($latitude1) * cos ($beta) +) / (cos ($latitude1) * sin ($beta)); if ($cosAzimuth >= 1.0) { $azimuth = 0.0; } elsif ($cosAzimuth <= -1.0) { $azimuth = 180.0; } else { $azimuth = acos ($cosAzimuth) * $radToDeg; } if (sin ($longitude2 - $longitude1) < 0.0) { $azimuth = 360.0 - $azimuth; } return (floor ($distance * 100.0) / 100.0, floor ($azimuth * 100 +.0) / 100.0); } # # If P1 Is North Or South Pole, Then Azimuth Is Undefined # if (sgn ($latitude1) == sgn ($latitude2)) { $distance = $earthRadius * ($pi / 2 - abs ($latitude2)); } else { $distance = $earthRadius * ($pi / 2 + abs ($latitude2)); } return (floor ($distance * 100.0) / 100.0, 0.0); } # # Why is there no intrinsic sign function in Perl? # sub sgn { return $_[0] == 0 ? 0 : $_[0] < 0 ? -1 : 1; }

In reply to Lat/Long distance calculator by jcwren

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-20 15:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found