Distance function help

• August 2nd, 2012, 04:53 PM
abf617
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:
Code :

```  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; }   }```

Code :

```  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!
• August 2nd, 2012, 06:32 PM
Zaphod_b
Re: Distance function help
Quote:

Originally Posted by abf617
... 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:

Code java:

```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