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 2 of 2

Thread: Distance function help

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Distance function help

    Hey guys,
    so I have Location class, and a radio class(the radio class contains the coordinates). Basically for a program I am writing I need the location to take the coordinates from the radio class, and use the distance formula for the 2 sets of coordinates.
    Location:
     
    public class Location {
     
     
    private double lat, lon;
     
     
    public Location (double lat, double lon){
    this.lat=lat;
    this.lon=lon;
    }
     
    public Location (){
    	this.lat=0.0;
    	this.lon=0.0;
    }
     
    public void setLat(double lat){
    	this.lat=lat;
    }
     
    public void setLon(double lon){
    	this.lon=lon;
    }
     
    public double Distance (double lat1, double lon1, double lat2, double lon2) {
     
    	lat1 = Math.toRadians(lat1);
    	lon1 = Math.toRadians(lon1);
    	lat2 = Math.toRadians(lat2);
    	lon2 = Math.toRadians(lon2);
     
    	double cosLat2 = Math.cos(lat2);
    	double sinLonChange = Math.sin(lon2-lon1);
    	double cosLat1 = Math.cos(lat1);
    	double sinLat2 = Math.sin(lat2);
    	double sinLat1 = Math.sin(lat1);
    	double cosLonChange = Math.cos(lon2-lon1);
     
    	double a = Math.pow((cosLat2*sinLonChange), 2);
    	double b = Math.pow(((cosLat1*sinLat2)-(sinLat1*cosLat2*cosLonChange)), 2);
    	double c = (sinLat1*sinLat2)+(cosLat1*cosLat2*cosLonChange);
     
    	double spherDistance = Math.atan(Math.sqrt(a+b)/c);
     
    	double Distance = 3959 * spherDistance;
     
    	return Distance;
     
    }
     
     
    public double getLat(){
    	return lat;
    }
     
    public double getLon(){
    	return lon;
    }
    public double getDistance(){
    	return Distance;
    }
     
    public String toString(){
    	String dist="lat"+lat+"lon"+lon;
    	return dist;
    }
     
    }

    and Radio:
     
    public class Radio {
    	public static void main(String[] args) {
    Location loc1=new Location(10,20);
    Location loc2=new Location(30,40);
     
    System.out.println(loc1.toString());	
    System.out.println(loc2.toString());	
     
    	}
     
     
    }

    Any help or suggestions would be greatly appreciated.
    Thank y'all very much!


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Distance function help

    Quote Originally Posted by abf617 View Post
    ... I need..to take the coordinates from the radio class, and use the distance formula for the 2 sets of coordinates...
    Your Distance function has nothing to do with the Location class, so could be made a static function of any class. (It doesn't make sense for a Location object to have a Distance data member, and yours does not so your Location class has a couple of problems that could be fixed if you really need it.)

    I mean, it might make sense to for the Location class to have a Distance function that tales another Location object and calculates distance from the current object, or a Distance function that takes latitude and longitude of somewhere else and calculates distance from the current object. Stuff like that.

    In the meanwhile, since the title that you gave the thread indicates to me that you need the Distance function to work, I think it already works. Just call it with latitudes and longitudes (in decimal degrees) for two places on Earth:

    public class Z
    {
        public static void main(String [] args)
        {
            // Example at [url]http://en.wikipedia.org/wiki/Great-circle_distance[/url]
            // gives results in km.  Your Distance function is set up for statute miles.
            // Distance is calculated from an airport in Nashville, Tennessee to
            // Los Angeles, California.
     
            double d = DistanceInStatuteMiles(36.12, -86.67, 33.94, -118.4);
            System.out.printf("d = %.2f statute miles.\n", d);
        } // End of main function
     
        static double DistanceInStatuteMiles(double lat1, double lon1, double lat2, double lon2)
        {
            lat1 = Math.toRadians(lat1);
            lon1 = Math.toRadians(lon1);
            lat2 = Math.toRadians(lat2);
            lon2 = Math.toRadians(lon2);
     
            double cosLat2 = Math.cos(lat2);
            double sinLonChange = Math.sin(lon2-lon1);
            double cosLat1 = Math.cos(lat1);
            double sinLat2 = Math.sin(lat2);
            double sinLat1 = Math.sin(lat1);
            double cosLonChange = Math.cos(lon2-lon1);
     
            double a = Math.pow((cosLat2*sinLonChange), 2);
            double b = Math.pow(((cosLat1*sinLat2)-(sinLat1*cosLat2*cosLonChange)), 2);
            double c = (sinLat1*sinLat2)+(cosLat1*cosLat2*cosLonChange);
     
            double spherDistance = Math.atan(Math.sqrt(a+b)/c);
     
            // Statute miles
            double distance = 3959 * spherDistance;
            // Change the constant to 6372.8 to get km. (Change the function name too.)
     
            return distance;
        } // End of Distance function
     
    } // End of class definition

    Output

    d = 1793.66 statute miles.

    Now if you have problems that you don't understand with the other classes, you can probably get help if you ask specific questions: Tell us what compiler messages and/or program output that you got and tell us what you don't understand.


    Cheers!

    Z
    Last edited by Zaphod_b; August 2nd, 2012 at 06:51 PM.

Similar Threads

  1. Getting Distance between two addresses
    By Shockwave786 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 23rd, 2012, 10:19 AM
  2. i am having a problem with the distance formula,help needed!
    By Bentino in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2012, 07:07 PM
  3. Java client development for distance learning.
    By bosozoku in forum Java Theory & Questions
    Replies: 0
    Last Post: March 4th, 2012, 10:01 AM
  4. Distance between 2 points
    By captain in forum Java Theory & Questions
    Replies: 3
    Last Post: February 22nd, 2012, 12:53 AM
  5. Memoization (dyanamic programming) for edit distance recursion
    By rph12 in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 28th, 2011, 10:37 AM

Tags for this Thread