Java Euclidean Distance euclideanDistance(double[] coord1, double[] coord2)

Here you can find the source of euclideanDistance(double[] coord1, double[] coord2)

Description

Calculates the Euclidean distance between two coordinates.

License

Open Source License

Parameter

Parameter Description
coord1 First Coordinate
coord2 Second Coordinate

Return

Euclidean Distance between coord1 and coord2

Declaration

public static double euclideanDistance(double[] coord1, double[] coord2) 

Method Source Code

//package com.java2s;
/**/*w  w w . j  a va 2s  .  com*/
 * 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 Euclidean distance between two coordinates.
     * 
     * Coordinates are represented as an array where the value for each dimension is in each index
     * 
     * @param coord1 First Coordinate
     * @param coord2 Second Coordinate
     * 
     * @return Euclidean Distance between coord1 and coord2
     */
    public static double euclideanDistance(double[] coord1, double[] coord2) {
        return distanceBase(coord1, coord2, 2);
    }

    /**
     * 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. euclideanDistance(double instance1[], double instance2[])
  2. euclideanDistance(double x1, double y1, double x2, double y2)
  3. euclideanDistance(double x1, double y1, double x2, double y2)
  4. EuclideanDistance(double xSource, double ySource, double xTarget, double yTarget)
  5. euclideanDistance(double[] a, double[] b)
  6. euclideanDistance(double[] data, double[] pattern)
  7. euclideanDistance(double[] l1, double[] l2, boolean weighted)
  8. euclideanDistance(double[] p, double[] q)
  9. euclideanDistance(double[] vector)