Haversine Formula

Discussion in 'PHP' started by pradeep, May 30, 2005.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Haversine Formula

    The haversine formula is an equation important in navigation, giving great-circle distances between two points on a sphere from their longitudes and latitudes. It is a special case of a more general formula in spherical trigonometry, the law of haversines, relating the sides and angles of spherical "triangles".

    (The formulas could equally be written in terms of any multiple of the haversine, such as the older versine function (twice the haversine). Historically, the haversine had, perhaps, a slight advantage in that its maximum is one, so that logarithmic tables of its values could end at zero. These days, the haversine form is also convenient in that it has no coefficient in front of the sin2 function.)

    PHP:
    # Where:
    #  $l1 ==> latitude1
    #  $o1 ==> longitude1
    #  $l2 ==> latitude2
    #  $o2 ==> longitude2
    function haversine ($l1$o1$l2$o2)
    {
        
    $l1 deg2rad ($l1);
        
    $sinl1 sin ($l1);
        
    $l2 deg2rad ($l2);
        
    $o1 deg2rad ($o1);
        
    $o2 deg2rad ($o2);
                    
        return (
    7926 26 $sinl1) * asin (min (10.707106781186548 sqrt ((- (sin ($l2) * $sinl1) - cos ($l1) * cos ($l2) * cos ($o2 $o1)))));
    }

    Further reading:
    http://www.answers.com/haversine%20formula
    http://en.wikipedia.org/wiki/Haversine_formula
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice