Java Distance Calculate distanceNd(double[] p1, double[] p2, double[] scratchSpace)

Here you can find the source of distanceNd(double[] p1, double[] p2, double[] scratchSpace)

Description

distance Nd

License

Open Source License

Declaration

public static double distanceNd(double[] p1, double[] p2, double[] scratchSpace) 

Method Source Code

//package com.java2s;

public class Main {
    public static double distanceNd(double[] p1, double[] p2, double[] scratchSpace) {
        if (p1.length != p2.length || p1.length != scratchSpace.length)
            throw new IllegalArgumentException("mismatched dimenions in distanceNd()");
        if (p1 == scratchSpace || p2 == scratchSpace)
            throw new IllegalArgumentException("scratch space must be dofferent from inputs");
        if (p1.length == 0)
            return 0;
        for (int i = 0; i < p1.length; i++) {
            scratchSpace[i] = Math.abs(p2[i] - p1[i]);
        }/*from www.j  a va 2 s. c om*/
        double max = scratchSpace[0];
        for (int i = 1; i < p1.length; i++) {
            max = Math.max(max, scratchSpace[i]);
        }
        if (max == 0)
            return 0;
        for (int i = 0; i < p1.length; i++) {
            scratchSpace[i] /= max;
        }
        double sumSq = 0;
        for (int i = 0; i < p1.length; i++) {
            sumSq += scratchSpace[i] * scratchSpace[i];
        }
        return max * Math.sqrt(sumSq);
    }
}

Related

  1. distanceKM(double lat1, double lng1, double lat2, double lng2)
  2. distanceLevenshtein(CharSequence s, CharSequence t)
  3. distanceLongitude(double latitude, double east, double west)
  4. distanceMat(double[][] m1, double[][] m2)
  5. distanceMeter(double prevLat, double prevLon, double currentLat, double currentLon)
  6. distancePointToLine(final double x0, final double y0, final double x1, final double y1, final double xp, final double yp)
  7. distancePointToPlane(final double x0, final double y0, final double z0, final double[] normal, final double xp, final double yp, final double zp)
  8. distancePointToPoint(final double x1, final double y1, final double x2, final double y2)
  9. distancePointToPoint(float x1, float y1, float x2, float y2)