java 我正在尝试使用大圆距离公式计算2个位置(或类)之间的距离公式

bmp9r5qi  于 2023-02-28  发布在  Java
关注(0)|答案(1)|浏览(126)

我有两个不同的类,分别模拟一个站点位置和另一个计算位置。这些类曾经是一个类中彼此的一部分,但由于老师的指示,我将它们分开。我在两个类中完成了大部分代码,但在最后我不得不使用距离计算和目标长/纬度将这两个类连接在一起时,我感到很挣扎。如果我能弄清楚如何编码以及代码在Location类中的含义,我可能就能自己弄清楚Station类部分了。

// 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
*/

我很难理解变量lat2lon2的用途。
公式为:
3959.0 * ArcCos[ sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1) ]
我假设第一个位置的坐标是(lat1, lon1),第二个位置的坐标是(lat2, lon2),那么很明显,这两个位置之间的距离可以用下面的公式来计算,我真的很困惑。

50few1ms

50few1ms1#

下面的代码是一个SSCCE。我添加了一个main方法,这样你就可以运行它了。你可能还想用调试器来运行它,以便更好地理解代码是如何工作的。
请注意,我必须对您问题中的代码进行一些更正,因为该代码无法编译。
我改变了类的名称,以免与你的代码冲突。因此,你可以复制这段代码,仍然保持你原来的代码。

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

相关问题