Java Utililty Methods Distance Calculate

List of utility methods to do Distance Calculate

Description

The list of methods to do Distance Calculate are organized into topic(s).

Method

doubledistanceCorrelation(final double[][] x, final double[][] y)
distance Correlation
int n = x.length;
if (n != y.length)
    throw new IndexOutOfBoundsException("x and y should be of same length");
double[][] a = new double[n][n], b = new double[n][n];
double[] a1m = new double[n], a2m = new double[n], b1m = new double[n], b2m = new double[n];
double am = 0, bm = 0;
for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
...
intdistanceDamerauLevenshtein(CharSequence source, CharSequence target)
distance Damerau Levenshtein
if (source == null || "".equals(source)) {
    return target == null || "".equals(target) ? 0 : target.length();
} else if (target == null || "".equals(target)) {
    return source.length();
int srcLen = source.length();
int targetLen = target.length();
int[][] distanceMatrix = new int[srcLen + 1][targetLen + 1];
...
doubledistanceFast(double lat1, double lon1, double lat2, double lon2)
distance Fast
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2))
        + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
return dist;
doubledistanceFrom(double lat1, double long1, double lat2, double long2)
Method calculating distance.
double R = 6371; 
double rLat1 = Math.toRadians(lat1);
double rLat2 = Math.toRadians(lat2);
double dLat = rLat2 - rLat1;
double dLon = Math.toRadians(long2 - long1);
double a = Math.sin(dLat / 2.0) * Math.sin(dLat / 2.0)
        + Math.sin(dLon / 2.0) * Math.sin(dLon / 2.0) * Math.cos(rLat1) * Math.cos(rLat2);
double c = 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
...
FloatDistanceFromLine(float cx, float cy, float ax, float ay, float bx, float by)
Distance From Line
float diffbax = bx - ax;
float diffbay = by - ay;
float diffcax = cx - ax;
float diffcay = cy - ay;
float diffcbx = cx - bx;
float diffcby = cy - by;
double r_numerator = diffcax * diffbax + diffcay * diffbay;
double r_denomenator = square(diffbax) + square(diffbay);
...
doubledistanceFromLineToPoint(double x1, double y1, double z1, double x2, double y2, double z2, double px, double py, double pz)
Get the shortest distance from a line segment to a point
double ddx = x2 - x1;
double ddy = y2 - y1;
double ddz = z2 - z1;
double lineMag = ddx * ddx + ddy * ddy + ddz * ddz;
double u = (((px - x1) * ddx) + ((py - y1) * ddy) + ((pz - z1) * ddz)) / lineMag;
if (u < 0.0f || u > 1.0f) {
    return -1.0; 
double ix = x1 + u * (x2 - x1);
double iy = y1 + u * (y2 - y1);
double iz = z1 + u * (z2 - z1);
ddx = ix - px;
ddy = iy - py;
ddz = iz - pz;
return Math.sqrt(ddx * ddx + ddy * ddy + ddz * ddz);
doubledistanceFromZero(int x, int y)
Calculate the distance between two 2D points.
return distance(0, 0, x, y);
DoubledistanceHaversine(Double lat1, Double lon1, Double lat2, Double lon2, String param)
distance Haversine
final Double earthRadius = 3960.00;
Double latDistance = lat2 - lat1;
Double lonDistance = lon2 - lon1;
Double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) + Math.cos(toRad(lat1))
        * Math.cos(toRad(lat2)) * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
Double distance = earthRadius * c;
if (param.equals("miles"))
...
doubledistanceInf(double[] p1, double[] p2)
Distance inf.
double max = 0;
for (int i = 0; i < p1.length; i++) {
    max = Math.max(max, Math.abs(p1[i] - p2[i]));
return max;
doubledistanceInMeters(double lat1, double lon1, double lat2, double lon2)
This calculation is not the most accurate, but it takes less computation time.
double dy = (lat2 - lat1) * METERS_PER_DEGREE;
double phi = (lat2 + lat1) * 0.017453 / 2;
double dx = (lon2 - lon1) * METERS_PER_DEGREE * Math.cos(phi);
return Math.hypot(dx, dy);