Amber Lyda
Amber Lyda

Reputation: 23

I am trying to calculate a distance formula between 2 locations (or classes) using the Great circle distance formula

I have 2 different classes that model a Station Location and the other a calculated Location. These classes used to be a part of each other in one class but I have separated them out due to instructions from my teacher. I have most of the code done in both classes but I am struggling at the very end when I have to join these 2 classes together somehow using the distance calculation and target long/lat.If I could get some clarity on how to code and what the code means in the Location class I can probably figure out Station class part on my own.

// Models a location
public class Location {
    // instant variables
    private double latitude;
    private double longitude;
    
    // constant (Approximate radius of earth in miles.
    public static final double EARTH_Radius = 3959.0;

    // Constructor for objects of class Location
    public Location(double inLatitude, double inLongitude) {
        this.latitude = inLatitude;
        this.Longitude = inLongitude;

    //returns latitude and longitude of the Location
    public double getLatitude() {
        return this.latitude;
    public double getLongitude() {
        return this.longitude();

// Now for the part that I am struggling with 
// Calculates approximate distance between the this location
     *    and the parameter value location.
     * 
     * @param  inLocation Location  input Location.
     * @return double  distance in miles
     */
    public double calcDistance(Location inLocation)
 // NOTE: be sure to update your calculation of distance
        // NOTE: consistent with the write up of Lab 2
        // replace this & NOTE comments with your code    
        
        
        // convert the latitude & longitude of this Location to radians
        // HINT: declare local variables to hold equivalent radians
        // HINT: use appropriate method of the MATH class
        // replace this & HINT comments with your code 
        
        // converts latitude of this Location to radians
        
        // converts longitude of this Location to radians
        
        

        // calculate the distance and return
        // HINT: use appropriate methods of the MATH class
        // HINT: used the defined 'final constant" for earth's radius
        // replace this & HINT comments & return statement with your code 
        
    }
// so in when it mentions being consistant with the write up and 
// this is the write up exactly 

/*
1.  First convert latitude and longitude (in degrees) to latitude and longitude in radians
NOTE: use the Math.toRadians() method
2.  Use the formula
3959.0 * ArcCos[  sin(lat1) *  sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1) ]
Use Math.acos() for ArcCos, Math.cos() for cosine, Math.sin() for sin
3959.0 is the approximate radius of the earth in miles
*/

I am having a hard time understanding the purpose of the variables of lat2 and lon2.

The formula is:

3959.0 * ArcCos[ sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1) ]

I am assuming the first location has coordinates of (lat1, lon1) and the second location has coordinates of (lat2, lon2) and then obviously the distance between both of these locations can be calculated with the formula I'm just really confused.

Upvotes: -1

Views: 134

Answers (1)

Abra
Abra

Reputation: 20923

The below code is a SSCCE. I added a main method so you can run it. You may also want to run it with a debugger to understand better how the code works.

Note that I had to make some corrections to the code in your question since that code does not compile.

I changed the name of the class so as not to clash with your code. Hence you can copy this code and still keep your original code.

public class Lokashun {
    private double latitude;
    private double longitude;
    
    // constant (Approximate radius of earth in miles.
    public static final double EARTH_Radius = 3959.0;

    public Lokashun(double inLatitude, double inLongitude) {
        latitude = inLatitude;
        longitude = inLongitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public double calcDistance(Lokashun inLocation) {
        double lat1 = Math.toRadians(latitude);
        double lat2 = Math.toRadians(inLocation.getLatitude());
        double lon1 = Math.toRadians(longitude);
        double lon2 = Math.toRadians(inLocation.getLongitude());
        double distance = Math.sin(lat1) * Math.sin(lat2) +
                          Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1);
        return EARTH_Radius * Math.acos(distance);
    }

    public static void main(String[] args) throws Exception {
        Lokashun la = new Lokashun(34.0522, 118.2437);
        Lokashun sf = new Lokashun(37.7749, 122.4194);
        System.out.println("LA 2 SF: " + la.calcDistance(sf));
    }
}

Upvotes: 0

Related Questions