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

doubledistFrom(double lat1, double lng1, double lat2, double lng2)
dist From
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2 - lat1);
double dLng = Math.toRadians(lng2 - lng1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1))
        * Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2) * Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return earthRadius * c;
doubledistHalf(double angA, double angB)
Angular distance between two half circle angles.
double a = Math.abs(angA - angB);
if (a <= Math.PI / 2)
    return a;
else
    return Math.PI - a;
doubledistHaversineRAD(double lat1, double lon1, double lat2, double lon2)
dist Haversine RAD
if (lat1 == lat2 && lon1 == lon2)
    return 0.0;
double hsinX = Math.sin((lon1 - lon2) * 0.5);
double hsinY = Math.sin((lat1 - lat2) * 0.5);
double h = hsinY * hsinY + (Math.cos(lat1) * Math.cos(lat2) * hsinX * hsinX);
return 2 * Math.atan2(Math.sqrt(h), Math.sqrt(1 - h));
StringdistinctFrom(String string, String other)
distinct From
if (string.equals(other)) {
    return 'a' + upcase(string);
return string;
doubledistL1(double[] h1, double[] h2)
dist L
double sum = 0d;
for (int i = 0; i < h1.length; i++) {
    sum += Math.abs(h1[i] - h2[i]);
return sum / h1.length;
doubledistL1(int[] h1, int[] h2)
Manhattan distance
double sum = 0d;
for (int i = 0; i < h1.length; i++) {
    sum += Math.abs(h1[i] - h2[i]);
return sum / h1.length;
doubledistortion(double[] xvalues, double[] yvalues, int[] include)
Returns the sum of the squared difference of xvalues and yvalues for non-zero include index values
double ddiff;
double dsumsq = 0;
for (int nindex = 0; nindex < include.length; nindex++) {
    if (include[nindex] != 0) {
        ddiff = (xvalues[nindex] - yvalues[nindex]);
        dsumsq += ddiff * ddiff;
return dsumsq;
doubledistpl(double A, double B, double C, double x, double y)
distpl
return Math.abs(A * x + B * y + C) / Math.sqrt(Math.pow(A, 2) + Math.pow(B, 2));
doubledistPointLineSigned(double px, double py, double qx, double qy, double rx, double ry)
Points to right of line segment have negative distance.
double magI = 1 / Math.sqrt(rx * rx + ry * ry);
double nx = -ry * magI;
double ny = rx * magI;
double c = -(qx * nx + qy * ny);
return px * nx + py * ny + c;
doubledistPointLineSquared(double px, double py, double qx, double qy, double rx, double ry)
Calculates square of min distance from point to an infinite line segment.
double sx = px - qx;
double sy = py - qy;
return dot(sx, sy, sx, sy) - distAlongRaySquared(px, py, qx, qy, rx, ry);