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

  • SEARCH
  • Home
  • 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 6049905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:37:44+00:00 2026-05-23T07:37:44+00:00

I borrowed the following method from somewhere on the internet (Can’t remember where). But

  • 0

I borrowed the following method from somewhere on the internet (Can’t remember where). But its doing a straight forward process, finding the distance between two gps points. It works just fine, except that it may be a little slow, as I’m running it across millions of points.
I was wondering if anyone knows an approach that would be computationally less expensive.

The accuracy needs to be in the general area of ‘correct’ but doesn’t need to be 100% accurate.

private double distFrom(double lat1, double lng1, double lat2, double lng2) {
    double earthRadius = 3958.75;
    double dLat = Math.toRadians(lat2-lat1);
    double dLng = Math.toRadians(lng2-lng1);
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
           Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
           Math.sin(dLng/2) * Math.sin(dLng/2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    return   earthRadius * c;
  }
}

P.s I did indeed find a number of other relevant questions, but they don’t really focus on my speed concern.

  • 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-05-23T07:37:45+00:00Added an answer on May 23, 2026 at 7:37 am

    If you don’t mind ignoring the slight oblateness of the Earth (and your posted Haversine code does just that anyway) consider pre-converting all of your spherical (lat/long) coordinates into 3D unit-length cartesian coordinates first, per:

    http://en.wikipedia.org/wiki/Spherical_coordinate_system

    Then your spherical distance between cartesian coordinates p1 and p2 is simply:

    r * acos(p1 . p2)
    

    Since p1 and p2 will have unit length this reduces to four multiplications, two additions and one inverse trig operation per pair.

    Also note that the calculation of dot products is an ideal candidate for optimisation, e.g. via GPU, MMX extensions, vector libraries, etc.

    Furthermore, if your intent is to order the pairs by distance, potentially ignoring more distant pairs, you can defer the expensive r*acos() part of the equation by sorting the list just on the dot product value since for all valid inputs (i.e. the range [-1, 1]) it’s guaranteed that:

    acos(x) < acos(y) if x > y
    

    You then just take the acos() of the values you’re actually interested in.

    Re: the potential inaccuracies with using acos(), those are really only significant if you’re using single-precision float variables. Using a double with 16 significant digits should get you distances accurate to within one metre or less.

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

Sidebar

Related Questions

I borrowed the code from http://jqueryui.com/demos/autocomplete/#combobox but don't know how to get the value
This following code example is borrowed from MSDN here . I am not getting
I borrowed some code from a site, but I don't know how to get
I want to chmod recursively in my build.xml and borrowed the following from this
This is borrowed from another question I made on the site, but would have
The following gist: https://gist.github.com/1876791 (borrowed from the demo in this question ) is a
I have borrowed code from this link PHP regex templating - find all occurrences
I am using the Entity Framework, ASP.NET and C#3.5 I borrowed the following code
Here is a piece of code borrowed from the Endogine engine. It is supposed
I am implementing a MyGestureDetector that extends a SimpleOnGestureListener. I borrowed the class from:

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.