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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

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

  1. #1
    Junior Member
    Join Date
    Apr 2018
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2018
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compute the distance between two points on the surface of earth

    Quote Originally Posted by Norm View Post

    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?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Compute the distance between two points on the surface of earth

    So how would I do the conversions?
    Multiply the degrees by radians/degree
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Compute the distance between two points on the surface of earth

    So how would I do the conversions?
    Multiply the degrees by radians/degree
    Check the Math class for a method
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Apr 2018
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compute the distance between two points on the surface of earth

    Quote Originally Posted by Norm View Post
    Multiply the degrees by radians/degree
    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. 
     
        //Math.toRadians() converts degrees into radians. 
     
        public static double d(double x1, double x2, double y1, double y2, double a) 
     
        { 
     
            x1 = Math.toRadians(x1); 
     
            y1 = Math.toRadians(y1); 
     
            x2 = Math.toRadians(x2); 
     
            y2 = Math.toRadians(y2); 
     
     
     
            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 
     
        } 
     
    }

Similar Threads

  1. Replies: 1
    Last Post: November 6th, 2013, 02:48 AM
  2. Distance between 2 points
    By captain in forum Java Theory & Questions
    Replies: 3
    Last Post: February 22nd, 2012, 12:53 AM
  3. Sphere (Volume/Surface Area)
    By Alex L in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 5th, 2011, 02:51 PM
  4. Creation of a curved surface as an airfoil using java AWT
    By murdplacid in forum AWT / Java Swing
    Replies: 2
    Last Post: September 22nd, 2010, 02:23 AM