Sample – compute distance between cities

by Nideesh C on February 8, 2011 · 0 comments

in C++




// Simple program to compute the distance between two cities
// given their coordinates

// May not work for some values (two points on opposite sides of
// the equator). Need to do some thinking about that.

#include<iostream.h>
#include<math.h>

float toRadian (float angle) {
float const PI = atan(1.0)*4.0;

return(angle * PI / 180.0);
}

float computeDist (float lat1, float long1, float lat2, float long2) {
float const R = 3964.0;
float temp;

temp = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(long1-long2);
return(2.0 * R * atan(sqrt((1.0-temp)/(1.0+temp))));
}

void main () {
float lat1, long1, lat2, long2, distance;

cout << “Enter lat1, long1: “;
cin >> lat1 >> long1;
cout << “Enter lat2, long2: “;
cin >> lat2 >> long2;
distance = computeDist(toRadian(lat1),
toRadian(long1),
toRadian(lat2),
toRadian(long2));
cout << “Distance = ” << distance << endl;
}

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. Sample – compute square roots using Newton’s method
  2. Sample – convert temperatures
  3. Sample – Fibonacci numbers
  4. Sample – factorial
  5. Sample program (multiplication using addition)

Leave a Comment

Previous post:

Next post: