Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2011/02/18

Capital Idea 2 - So Far Away

Rather than query Google Maps to get driving distances between capitals, I found latitude and longitude and use these functions to determine the distance, then ran it against all thousand-some possible distances, so it is ultimately a hash check rather than a calculation to find a distance.

sub haversine {
    my ( $lat1, $lon1, $lat2, $lon2 ) = @_ ;

    my $theta = $lon1 - $lon2 ;
    my $dist =
        sin( deg2rad( $lat1 ) ) *
        sin( deg2rad( $lat2 ) ) +
        cos( deg2rad( $lat1 ) ) *
        cos( deg2rad( $lat2 ) ) *
        cos( deg2rad( $theta ) ) ;

    $dist = acos( $dist ) ;
    $dist = rad2deg( $dist ) ;
    $dist = $dist * 60 * 1.1515 ;
    return $dist ;
    }

sub acos {
    my ( $rad ) = @_ ;
    my $ret = atan2( sqrt( 1 - $rad**2 ), $rad ) ;
    return $ret ;
    }

sub deg2rad {
    my ( $deg ) = @_ ;
    return ( $deg * $pi / 180 ) ;
    }

sub rad2deg {
    my ( $rad ) = @_ ;
    return ( $rad * 180 / $pi ) ;
    }

2 comments:

  1. Why not use Geo::Distance? And if speed is an issue, there's an XS version as well.

    ReplyDelete
  2. Entirely because I had not heard of it before. Searching "Perl distance latitude longitude" did not bring it up to the top. I figured that, within reason, the basic algorithm would be simple enough for even a barely-got-through-the-math programmer like me.

    Since there are a known and finite number of distances to be determined, it's easier to compute them once and store them than to recompute them on the fly. I have two DB tables, one holding the length of each line and one holding the two end points of each line, which I import as a hash, so I have all the data I need going in. I just have to find a better than the run-until-the-sun-goes-cold algorithm I'm using now.

    ReplyDelete