Java tutorial
//package com.java2s; /* * Copyright (C) 2013 The Android Open Source Project * * 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 { /** * Gets the relative bearing from one geographical coordinate to another. * * @param latitude1 the latitude of the source point * @param longitude1 the longitude of the source point * @param latitude2 the latitude of the destination point * @param longitude2 the longitude of the destination point * @return the relative bearing from point 1 to point 2, in degrees. The result is guaranteed * to fall in the range 0-360 */ public static float getBearing(double latitude1, double longitude1, double latitude2, double longitude2) { latitude1 = Math.toRadians(latitude1); longitude1 = Math.toRadians(longitude1); latitude2 = Math.toRadians(latitude2); longitude2 = Math.toRadians(longitude2); double dLon = longitude2 - longitude1; double y = Math.sin(dLon) * Math.cos(latitude2); double x = Math.cos(latitude1) * Math.sin(latitude2) - Math.sin(latitude1) * Math.cos(latitude2) * Math.cos(dLon); double bearing = Math.atan2(y, x); return mod((float) Math.toDegrees(bearing), 360.0f); } /** * Calculates {@code a mod b} in a way that respects negative values (for example, * {@code mod(-1, 5) == 4}, rather than {@code -1}). * * @param a the dividend * @param b the divisor * @return {@code a mod b} */ public static int mod(int a, int b) { return (a % b + b) % b; } /** * Calculates {@code a mod b} in a way that respects negative values (for example, * {@code mod(-1, 5) == 4}, rather than {@code -1}). * * @param a the dividend * @param b the divisor * @return {@code a mod b} */ public static float mod(float a, float b) { return (a % b + b) % b; } }