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

floatdistanceToSegmentSquared(float px, float py, float vx, float vy, float wx, float wy)
_more_
float l2 = distanceBetweenPoints(vx, vy, wx, wy);
if (l2 == 0) {
    return distanceBetweenPoints(px, py, vx, vy);
float t = ((px - vx) * (wx - vx) + (py - vy) * (wy - vy)) / l2;
if (t < 0) {
    return distanceBetweenPoints(px, py, vx, vy);
if (t > 1) {
    return distanceBetweenPoints(px, py, wx, wy);
return distanceBetweenPoints(px, py, (vx + t * (wx - vx)), (vy + t * (wy - vy)));
doubledistanceVincenty(final double lat1, final double lon1, final double lat2, final double lon2)
Calculates geodetic distance between two points specified by latitude/longitude using Vincenty inverse formula for ellipsoids

Vincenty Inverse Solution of Geodesics on the Ellipsoid (c) Chris Veness 2002-2010

from: Vincenty inverse formula - T Vincenty, "Direct and Inverse Solutions of Geodesics on the Ellipsoid with application of nested equations", Survey Review, vol XXII no 176, 1975

http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf

javascript source location: http://www.movable-type.co.uk/scripts/latlong-vincenty.html

final double L = Math.toRadians(lon2 - lon1);
final double U1 = Math.atan((1 - ABPLATTUNG_F) * Math.tan(Math.toRadians(lat1)));
final double U2 = Math.atan((1 - ABPLATTUNG_F) * Math.tan(Math.toRadians(lat2)));
final double sinU1 = Math.sin(U1), cosU1 = Math.cos(U1);
final double sinU2 = Math.sin(U2), cosU2 = Math.cos(U2);
double lambda = L, lambdaP = 2 * Math.PI;
int iterLimit = 20;
double cosSqAlpha = 0, sinSigma = 0, cos2SigmaM = 0, cosSigma = 0, sigma = 0;
...
intdistancia(int[] c1, int[] c2)
distancia
int distancia = 0;
for (int i = 0; i < c1.length; i++) {
    if (c1[i] != c2[i]) {
        distancia++;
return distancia;
doubledistAngle(double[] pos1, double[] pos2)
Distance between two angles
double[] xyzPos1 = radec2xyz(pos1);
double[] xyzPos2 = radec2xyz(pos2);
double dot = xyzPos1[0] * xyzPos2[0] + xyzPos1[1] * xyzPos2[1] + xyzPos1[2] * xyzPos2[2];
return Math.acos(dot / (normVector(xyzPos1) * normVector(xyzPos2)));
doubledistBetween(float x1, float y1, float x2, float y2)
dist Between
float dx = x1 - x2;
float dy = y1 - y2;
double diff = Math.sqrt((dx * dx) + (dy * dy));
return diff;
doubledistBetweenSq(double x1, double y1, double z1, double x2, double y2, double z2)
dist Between Sq
return Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2) + Math.pow(z1 - z2, 2);
booleandistence(double lat1, double lon1, double lat2, double lon2, double radius)
distence
double distence = GetDistance(lon1, lat1, lon2, lat2);
return (distence - radius < 0 ? true : false);
floatdistEucl(float[] x1, float[] x2)
dist Eucl
float distEucl = 0;
for (int i = 0; i < x1.length; i++) {
    distEucl = distEucl + (float) Math.pow(x2[i] - x1[i], 2);
distEucl = (float) Math.pow(distEucl, 0.5);
if (x1.length != x2.length) {
    return Float.NaN;
} else {
...
intdistExtraBits(int dist)
dist Extra Bits
if (dist < 5) {
    return 0;
} else if (dist < 9) {
    return 1;
} else if (dist < 17) {
    return 2;
} else if (dist < 33) {
    return 3;
...
doubledistFrom(double lat1, double lng1, double lat2, double lng2)
Returns the distance between 2 points in meters
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2 - lat1);
double dLng = Math.toRadians(lng2 - lng1);
double sindLat = Math.sin(dLat / 2);
double sindLng = Math.sin(dLng / 2);
double a = Math.pow(sindLat, 2)
        + Math.pow(sindLng, 2) * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
...