Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (C) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    private static int EARTH_RADIUS_KM = 6371;

    /**
     * Waypoint projection using haversine formula
     * http://en.wikipedia.org/wiki/Haversine_formula See discussion here for
     * further information: http://www.movable-type.co.uk/scripts/latlong.html
     */
    public static double[] project(double distance, double bearing, double startLat, double startLon) {

        double distanceRad = distance / EARTH_RADIUS_KM;
        double bearingRad = Math.toRadians(bearing);
        double startLatRad = Math.toRadians(startLat);
        double startLonRad = Math.toRadians(startLon);

        double endLat = Math.asin(Math.sin(startLatRad) * Math.cos(distanceRad)
                + Math.cos(startLatRad) * Math.sin(distanceRad) * Math.cos(bearingRad));

        double endLon = startLonRad
                + Math.atan2(Math.sin(bearingRad) * Math.sin(distanceRad) * Math.cos(startLatRad),
                        Math.cos(distanceRad) - Math.sin(startLatRad) * Math.sin(endLat));

        // Adjust projections crossing the 180th meridian:
        double endLonDeg = Math.toDegrees(endLon);

        if (endLonDeg > 180 || endLonDeg < -180) {
            endLonDeg = endLonDeg % 360; // Just in case we circle the earth
                                         // more than once.
            if (endLonDeg > 180) {
                endLonDeg = endLonDeg - 360;
            } else if (endLonDeg < -180) {
                endLonDeg = endLonDeg + 360;
            }
        }

        double[] endCoords = new double[] { Math.toDegrees(endLat), endLonDeg };

        return endCoords;

    }
}