Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Compute the distance between two points on the surface of earth

1. ## Compute the distance between two points on the surface of earth

I wanted to know if my code for finding the distance between two points on the surface of Earth is correct. I had to research online and found the formula for the great circle distance formula for the shortest path between two points on a sphere (I didn't really go in depth, but I do understand the concept of great circle distance and I am familiar with some of the math concepts from Multivariable Calculus and Linear Algebra, though that doesn't matter that much here), which was

d = acos^-1[cos(x1)cos(x2)cos(y1 - y2) + sin(x1)sin(x2)]
where a is the radius of the Earth (here, in kilometers), (x1, y1) are the coordinates for distance 1 and (x2, y2) are the coordinates for distance 2.

I want to know if my code looks fine, because I looked at the solution for this problem and it was kind of different from mines. Also, how would go about this problem, out of curiosity?:

```import java.lang.Math;
import java.util.Scanner;

class RicMain
{
public static void main(String args[])
{
Scanner ip = new Scanner(System.in);
double x1, x2, y1, y2, a, d;

//a is the radius of the sphere.
a = 6371.01; //radius of Earth is 6371.01 km.

System.out.print("Input the latitude of coordinate 1: ");
x1 = ip.nextDouble();
System.out.print("Input the longitude of coordinate 1: ");
y1 = ip.nextDouble();
System.out.print("Input the latitude of coordinate 2: ");
x2 = ip.nextDouble();
System.out.print("Input the longitude of coordinate 2: ");
y2 = ip.nextDouble();
//x1 and x2 are the latitude (x-axis).
//y1 and y2 are the longitude (y-axis).

//d is the great circle distance (distance between the two points (x1, y1) and (x2, y2).
d = a * Math.atan((Math.cos(x1)*Math.cos(x2)*Math.cos(y1 - y2)) + (Math.sin(x1)*Math.sin(x2)));
System.out.println("\nThe distance between points (x1, y1) and (x2, y2) is " + d);
}
}```

Also, I inputted the following:
Input the latitude of coordinate 1: 25
Input the longitude of coordinate 1: 35
Input the latitude of coordinate 2: 35.5
Input the longitude of coordinate 2: 25.5

The output is suppose to be: 1480.0848451069087 km

But my output was: 4157.795646651634

Not sure what's wrong. I think I have to convert it or something?  Reply With Quote

2. ## Re: Compute the distance between two points on the surface of earth

My output is: The distance between points (x1, y1) and (x2, y2) is 3839.512066956892

Finding the correct formula and then coding it can be a pain.

One problem probably is the args for the trig methods:
a - an angle, in radians.
You need to convert the degrees to radians.  Reply With Quote

3. ## Re: Compute the distance between two points on the surface of earth Originally Posted by Norm One problem probably is the args for the trig methods:

You need to convert the degrees to radians.
Yes, that was what I was thinking. I was unsure at first if my output was in degrees or radians by default. So how would I do the conversions?  Reply With Quote

4. ## Re: Compute the distance between two points on the surface of earth

So how would I do the conversions?  Reply With Quote

5. ## Re: Compute the distance between two points on the surface of earth

So how would I do the conversions?
Check the Math class for a method  Reply With Quote

6. ## Re: Compute the distance between two points on the surface of earth Originally Posted by Norm Check the Math class for a method
There was a mistake in the formula. It was suppose to be arccos, not arctan. I did that by accident. I made two new classes, the first one has the fixed formula with the degrees to radian conversion, and the other is using a nested class. They yield different outputs, but the nested class one is closer to the desired output when I input the following:

Input Data:
Input the latitude of coordinate 1: 25
Input the longitude of coordinate 1: 35
Input the latitude of coordinate 2: 35.5
Input the longitude of coordinate 2: 25.5

Expected Output

The distance between those points is: 1480.0848451069087 km

```import java.lang.Math;

import java.util.Scanner;

class RicMain

{

public static void main(String args[])

{

Scanner ip = new Scanner(System.in);

double x1, x2, y1, y2, a, d;

a = 6371.01;  //a is the radius of the sphere.

//radius of Earth is 6371.01 km.

System.out.print("Input the latitude of coordinate 1: ");

x1 = ip.nextDouble();

System.out.print("Input the longitude of coordinate 1: ");

y1 = ip.nextDouble();

System.out.print("Input the latitude of coordinate 2: ");

x2 = ip.nextDouble();

System.out.print("Input the longitude of coordinate 2: ");

y2 = ip.nextDouble();

//x1 and x2 are the latitude (x-axis) in degrees.

//y1 and y2 are the longitude (y-axis) in degrees.

//d is the great circle distance (distance between the two points (x1, y1) and (x2, y2).

d = a * Math.acos((Math.cos(x1)*Math.cos(x2)*Math.cos(y1 - y2)) + (Math.sin(x1)*Math.sin(x2)));

System.out.println("\nThe distance between points (x1, y1) and (x2, y2) is " + Math.toRadians(d) + " km");

}

}```

```import java.lang.Math;

import java.util.Scanner;

class RicMain

{

public static void main(String args[])

{

Scanner ip = new Scanner(System.in);

double x1, y1, x2, y2;

System.out.print("Input the latitude of coordinate 1: ");

x1 = ip.nextDouble();

System.out.print("Input the longitude of coordinate 1: ");

y1 = ip.nextDouble();

System.out.print("Input the latitude of coordinate 2: ");

x2 = ip.nextDouble();

System.out.print("Input the longitude of coordinate 2: ");

y2 = ip.nextDouble();

//x1 and x2 are the latitude (x-axis) and measured in degrees.

//y1 and y2 are the longitude (y-axis) and measured in degrees.

System.out.println("\nThe distance between points (x1, y1) and (x2, y2) is " + d(x1, y1, x2, y2, y2) + "km");

}

//this inner class converts points to radians.

public static double d(double x1, double x2, double y1, double y2, double a)

{

a = 6371.01; //radius of Earth is 6371.01 km.

//a is the radius of the sphere.

return a * Math.acos((Math.cos(x1)*Math.cos(x2)*Math.cos(y1 - y2)) + (Math.sin(x1)*Math.sin(x2)));

//the great circle distance (distance between the two points (x1, y1) and (x2, y2).

//is given in degrees

}

}```  Reply With Quote