Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8512115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:11:40+00:00 2026-06-11T04:11:40+00:00

I’m trying to port some javascript to php from http://www.movable-type.co.uk/scripts/latlong.html . I get it

  • 0

I’m trying to port some javascript to php from http://www.movable-type.co.uk/scripts/latlong.html. I get it to run with out error but it gives me a different value. I tried to double check it all and finally just did it again but with the same result. I’m not sure if i didn’t translate a function incorrectly somewhere or what.

The javascript…

/**
 * Returns the point of intersection of two paths defined by point and bearing
 *
 *   see http://williams.best.vwh.net/avform.htm#Intersection
 *
 * @param   {LatLon} p1: First point
 * @param   {Number} brng1: Initial bearing from first point
 * @param   {LatLon} p2: Second point
 * @param   {Number} brng2: Initial bearing from second point
 * @returns {LatLon} Destination point (null if no unique intersection defined)
 */
LatLon.intersection = function(p1, brng1, p2, brng2) {
  brng1 = typeof brng1 == 'number' ? brng1 : typeof brng1 == 'string' && trim(brng1)!='' ? +brng1 : NaN;
  brng2 = typeof brng2 == 'number' ? brng2 : typeof brng2 == 'string' && trim(brng2)!='' ? +brng2 : NaN;
  lat1 = p1._lat.toRad(), lon1 = p1._lon.toRad();
  lat2 = p2._lat.toRad(), lon2 = p2._lon.toRad();
  brng13 = brng1.toRad(), brng23 = brng2.toRad();
  dLat = lat2-lat1, dLon = lon2-lon1;

  dist12 = 2*Math.asin( Math.sqrt( Math.sin(dLat/2)*Math.sin(dLat/2) + 
    Math.cos(lat1)*Math.cos(lat2)*Math.sin(dLon/2)*Math.sin(dLon/2) ) );
  if (dist12 == 0) return null;

  // initial/final bearings between points
  brngA = Math.acos( ( Math.sin(lat2) - Math.sin(lat1)*Math.cos(dist12) ) / 
    ( Math.sin(dist12)*Math.cos(lat1) ) );
  if (isNaN(brngA)) brngA = 0;  // protect against rounding
  brngB = Math.acos( ( Math.sin(lat1) - Math.sin(lat2)*Math.cos(dist12) ) / 
    ( Math.sin(dist12)*Math.cos(lat2) ) );

  if (Math.sin(lon2-lon1) > 0) {
    brng12 = brngA;
    brng21 = 2*Math.PI - brngB;
  } else {
    brng12 = 2*Math.PI - brngA;
    brng21 = brngB;
  }

  alpha1 = (brng13 - brng12 + Math.PI) % (2*Math.PI) - Math.PI;  // angle 2-1-3
  alpha2 = (brng21 - brng23 + Math.PI) % (2*Math.PI) - Math.PI;  // angle 1-2-3

  if (Math.sin(alpha1)==0 && Math.sin(alpha2)==0) return null;  // infinite intersections
  if (Math.sin(alpha1)*Math.sin(alpha2) < 0) return null;       // ambiguous intersection

  //alpha1 = Math.abs(alpha1);
  //alpha2 = Math.abs(alpha2);
  // ... Ed Williams takes abs of alpha1/alpha2, but seems to break calculation?

  alpha3 = Math.acos( -Math.cos(alpha1)*Math.cos(alpha2) + 
                       Math.sin(alpha1)*Math.sin(alpha2)*Math.cos(dist12) );
  dist13 = Math.atan2( Math.sin(dist12)*Math.sin(alpha1)*Math.sin(alpha2), 
                       Math.cos(alpha2)+Math.cos(alpha1)*Math.cos(alpha3) )
  lat3 = Math.asin( Math.sin(lat1)*Math.cos(dist13) + 
                    Math.cos(lat1)*Math.sin(dist13)*Math.cos(brng13) );
  dLon13 = Math.atan2( Math.sin(brng13)*Math.sin(dist13)*Math.cos(lat1), 
                       Math.cos(dist13)-Math.sin(lat1)*Math.sin(lat3) );
  lon3 = lon1+dLon13;
  lon3 = (lon3+3*Math.PI) % (2*Math.PI) - Math.PI;  // normalise to -180..+180º

  return new LatLon(lat3.toDeg(), lon3.toDeg());
}

And my php…

<?php

function intersect($p1_lat, $p1_lon, $brng1, $p2_lat, $p2_lon, $brng2) {
    $lat1 = deg2rad($p1_lat);
    $lon1 = deg2rad($p1_lon);
    $lat2 = deg2rad($p2_lat);
    $lon2 = deg2rad($p2_lon);
    $brng13 = deg2rad($brng1);
    $brng23 = deg2rad($brng2);
    $dLat = $lat2 - $lat1;
    $dLon = $lon2 - $lon1;

    $dist12 = 2 * asin(sqrt(sin($dLat / 2) * sin($dLat / 2) +
                            cos($lat1) * cos($lat2) * sin($dLon / 2) * sin($dLon / 2)));
    if ($dist12 == 0) {
        return false;
    }

    // initial/final bearings between points
    $brngA = acos(( sin($lat2) - sin($lat1) * cos($dist12) ) /
            ( sin($dist12) * cos($lat1) ));
    if (is_nan($brngA)) {
        $brngA = 0;  // protect against rounding
    }
    $brngB = acos(( sin($lat1) - sin($lat2) * cos($dist12) ) /
            ( sin($dist12) * cos($lat2) ));

    if (sin($lon2 - $lon1) > 0) {
        $brng12 = $brngA;
        $brng21 = 2 * pi() - $brngB;
    } else {
        $brng12 = 2 * pi() - $brngA;
        $brng21 = $brngB;
    }

    $alpha1 = ($brng13 - $brng12 + pi()) % (2 * pi()) - pi();  // angle 2-1-3
    $alpha2 = ($brng21 - $brng23 + pi()) % (2 * pi()) - pi();  // angle 1-2-3

    if (sin($alpha1) == 0 && sin($alpha2) == 0) {
        return false;  // infinite intersections
    }
    if (sin($alpha1) * sin($alpha2) < 0) {
        return false;       // ambiguous intersection
    }

    $alpha3 = acos(-cos($alpha1) * cos($alpha2) +
            sin($alpha1) * sin($alpha2) * cos($dist12));
    $dist13 = atan2( sin($dist12)*sin($alpha1)*sin($alpha2),cos($alpha2)+cos($alpha1)*cos($alpha3) );
    $lat3 = asin(sin($lat1) * cos($dist13) +
            cos($lat1) * sin($dist13) * cos($brng13));
    $dLon13 = atan2(sin($brng13) * sin($dist13) * cos($lat1), cos($dist13) - sin($lat1) * sin($lat3));
    $lon3 = $lon1 + $dLon13;
    $lon3 = ($lon3 + 3 * pi()) % (2 * pi()) - pi();  // normalise to -180..+180º

    return array(rad2deg($lat3),rad2deg($lon3));
}

print_r (intersect(34.8403183513,-111.8159478164,148,34.8403254442,-111.8158955968,195));
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:11:41+00:00Added an answer on June 11, 2026 at 4:11 am

    I was trying to port another function from the same library and the issue I had was that I needed to use fmod() instead of the regular % modulus operator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm trying to create an if statement in PHP that prevents a single post
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.