Click here to Skip to main content
12,348,925 members (68,242 online)
Click here to Skip to main content
Add your own
alternative version

Stats

29.4K views
1 download
22 bookmarked
Posted

Distance using Longitiude and latitude using c++

, 27 Dec 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
exactly what the title says
Title:       Distance using Longitude and latitude
Author:      Chhibs 
Email:       annum0@gmail.com
Member ID:   12345
Language:    C++
Platform:    Windows, Linux
Technology:  WiMAX,WiFi, WDM
Level:       Intermediate, Advanced
Description: A quick code dump for haversine formula 
Section      Language c++
SubSection   Howto

Introduction

I have seen code for calculating distance using the haversine formula using C# etc on the site, but nothing using c++, so here is the code that just does that

Background (optional)

I saw couple fo articles doing thie distance calculation using the haversine formula but using .Net instead, since I code in C++(no .Net until really needed), I ported to c++ and below is the code

From math forum

http://mathforum.org/library/drmath/view/51879.html

Presuming a spherical Earth with radius R (see below), and that the
locations of the two points in spherical coordinates (longitude and
latitude) are lon1,lat1 and lon2,lat2, then the Haversine Formula 
(from R. W. Sinnott, "Virtues of the Haversine," Sky and Telescope, 
vol. 68, no. 2, 1984, p. 159): 

  dlon = lon2 - lon1
  dlat = lat2 - lat1
  a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2
  c = 2 * atan2(sqrt(a), sqrt(1-a)) 
  d = R * c

will give mathematically and computationally exact results. The 
intermediate result c is the great circle distance in radians. The 
great circle distance d will be in the same units as R.

Using the code

I am basically just posting a snippet of code that I use in a class which does the distance calculation for me
// Sample format for latitide and longitudes
// double lat1=45.54243333333333,lat2=45.53722222,long1=-122.96045277777778,long2=-122.9630556;
// Below is the main code
        #include <span class="code-keyword"><cmath></span>


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

chhibs
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralThanks Pin
Steve Gee27-Jun-09 12:24
memberSteve Gee27-Jun-09 12:24 
GeneralMathForum Pin
yarp28-Dec-07 20:05
memberyarp28-Dec-07 20:05 
GeneralYour title word "Longitiude" spell wrong. Pin
Jamesmeng28-Dec-07 14:11
memberJamesmeng28-Dec-07 14:11 
GeneralRe: Your title word "Longitiude" spell wrong. Pin
Mattamoo28-Dec-07 17:50
memberMattamoo28-Dec-07 17:50 
GeneralRe: Your title word "Longitiude" spell wrong. Pin
Mattamoo28-Dec-07 17:58
memberMattamoo28-Dec-07 17:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 27 Dec 2007
Article Copyright 2007 by chhibs
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid