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 1044613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:51:21+00:00 2026-05-16T15:51:21+00:00

I’m trying to convert spherical coordinates (namely latitude and longitude from a GPS device)

  • 0

I’m trying to convert spherical coordinates (namely latitude and longitude from a GPS device) into Cartesian coordinates. I’m following this simple conversion derived from the polar coordinates conversion equations.

Then I’m calculating the distance between the two point applying the euclidean distance but the value I’m finding is not always the same as the distance I can calculate using the haversine formula. In particular I’m noticing that given different longitudes but same latitudes leads to the same distances computed by the two algorithms, whereas having the same longitude and changing the latitude carries different values.

Here is the C code I am using:

double ComputeDistance(double lat1,double lon1, double lat2, double lon2)
{
    
    double dlon, dlat, a, c;
    dlon = lon2- lon1;
    dlat = lat2 - lat1;
    a = pow(sin(dlat/2),2) + cos(lat1) * cos(lat2) * pow(sin(dlon/2),2);
    c = 2 * atan2(sqrt(a), sqrt(1-a));
    return 6378140 * c;  /* 6378140 is the radius of the Earth in meters*/

    
}
int main (int argc, const char * argv[]) {

    double lat1 = 41.788251028649575;
    double lat2 = 41.788251028649575;
    double long1 = -118.1457209154;
    double long2 = -118.1407209154;//just ~10 meters distant
    
    lat1 = DEGREES_TO_RADIANS(lat1);
    lat2 = DEGREES_TO_RADIANS(lat2);
    long1 = DEGREES_TO_RADIANS(long1);
    long2 = DEGREES_TO_RADIANS(long2);
    
    //transform in cartesian coordinates
    double x = 6378140 * cos(lat1) * cos(long1);
    double y = 6378140 * cos(lat1) * sin(long1);
    
    double x2 = 6378140 * cos(lat2) * cos(long2);
    double y2 = 6378140 * cos(lat2) * sin(long2);

    
    double dist = sqrt(pow(x2 - x, 2) + pow(y2 - y, 2));
    printf("DIST %lf\n", dist);
    printf("NDIST %lf\n", ComputeDistance(lat1, long1, lat2, long2));

    return 0;
}

Am I doing something incorrect or there is some math behind it I am not seeing (and maybe ask this on Mathoverflow boards?). UPDATE It is not necessary to cross boards, as someone correctly pointed this conversion is not meaningful for computing the exact distance between two points (the distance between the two poles is zero). So I am reformulating it as: why at small deltas (0.0001 which corresponds to 10 mts more or less) of latitudes the distance appear to be so different from the haversine formula (20-25%)?

UPDATE 2:
As Oli Charlesworth pointed out, not considering the z axis makes this conversion a projection that does not mind the north-south difference. This is also the cause of the difference in deltas I was pointing out. In fact, in the correct transform the z is related to the latitude and if you consider it , then compute the euclidean distance between the two points (in a 3d space now), both latitude and longitude will lead to a good approximation for small deltas.
For Example for a degree of latitude the error is ~ 1.41 meters.

  • 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-16T15:51:21+00:00Added an answer on May 16, 2026 at 3:51 pm

    From this, there is no 2D map projection where distance is preserved. Calculating the distance from the 2D projection of points is useless.

    • 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
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.