Java Distance Calculate distanceBase(double[] coord1, double[] coord2, int order)

Here you can find the source of distanceBase(double[] coord1, double[] coord2, int order)

Description

Calculates the distance between two coordinates.

License

Open Source License

Parameter

Parameter Description
coord1 First Coordinate
coord2 Second Coordinate
order Order for the distance calculation. Used to calculate the power of each dimension and the n-th root for the final distance calculation

Return

Distance between coord1 and coord2

Declaration

private static final double distanceBase(double[] coord1, double[] coord2, int order) 

Method Source Code

//package com.java2s;
/**/*  w  w w .jav  a2 s  .  co  m*/
 * Copyright (C) 2014 Aniruddh Fichadia
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If you use or enhance the code, please let me know using the provided author information or via
 * email Ani.Fichadia@gmail.com.
 */

public class Main {
    /**
     * Calculates the distance between two coordinates. Is used as a base function for
     * euclideanDistance(double[], double[]) and minkowskiDistance(double[], double[])
     * 
     * Coordinates are represented as an array where the value for each dimension is in each index
     * 
     * @param coord1 First Coordinate
     * @param coord2 Second Coordinate
     * @param order Order for the distance calculation. Used to calculate the power of each
     *            dimension and the n-th root for the final distance calculation
     * 
     * @return Distance between coord1 and coord2
     */
    private static final double distanceBase(double[] coord1, double[] coord2, int order) {
        if (coord1.length != coord2.length)
            throw new IllegalArgumentException("Number of dimensions is not equal");

        final int NUM_DIMENSIONS = coord1.length;
        double distance = 0;

        for (int d = 0; d < NUM_DIMENSIONS; d++) {
            double absDiff = Math.abs(coord2[d] - coord1[d]);
            distance += Math.pow(absDiff, order);
        }

        distance = Math.pow(distance, (1.0 / order));
        ;

        return distance;
    }
}

Related

  1. distance_Sphere(double rlon1, double rlat1, double rlon2, double rlat2)
  2. distance_to_endpoint(int x1, int y1, int x2, int y2, int x, int y)
  3. distance_to_endpoint(int x1, int y1, int x2, int y2, int x, int y)
  4. distance_to_line(double x1, double y1, double x2, double y2, double x3, double y3)
  5. distanceAbs(final int a, final int b)
  6. distanceBetween(double latitude1, double longitude1, double latitude2, double longitude2)
  7. distanceBetween(double lon, double lat, double otherLon, double otherLat)
  8. distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude)
  9. distanceBetween(double x1, double y1, double x2, double y2)