001/**
002 * Copyright 2018 Tampere University of Technology, Pori Department
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package service.tut.pori.apilta.utils;
017
018/**
019 * Math utils
020 *
021 */
022public final class MathUtils {
023  private static final double R = 6372.8; // In kilometers
024  
025  /**
026   * 
027   * @param lat1
028   * @param lon1
029   * @param lat2
030   * @param lon2
031   * @return distance in km
032   */
033    public static double haversine(double lat1, double lon1, double lat2, double lon2) {
034        double dLat = Math.toRadians(lat2 - lat1);
035        double dLon = Math.toRadians(lon2 - lon1);
036        lat1 = Math.toRadians(lat1);
037        lat2 = Math.toRadians(lat2);
038 
039        double a = Math.pow(Math.sin(dLat / 2),2) + Math.pow(Math.sin(dLon / 2),2) * Math.cos(lat1) * Math.cos(lat2);
040        double c = 2 * Math.asin(Math.sqrt(a));
041        return R * c;
042    }
043
044    /**
045     * 
046     */
047    private MathUtils() {
048      // nothing needed
049    }
050}