Java Distance between Points getDistance(Point2D point1, Point2D point2)

Here you can find the source of getDistance(Point2D point1, Point2D point2)

Description

Compute the beeline distance between two points given in signed decimal coordinates wrapped in point objects, with the longitude in the X component and the latitude in the Y component.

License

Open Source License

Parameter

Parameter Description
point1 the first point
point2 the second point

Return

the distance between the two points, in meters

Declaration

public static double getDistance(Point2D point1, Point2D point2) 

Method Source Code


//package com.java2s;
import java.awt.geom.Point2D;

public class Main {
    /**/*from   w  ww  . j  a  v  a 2s .  c  o  m*/
     * Compute the beeline distance between two points given in signed decimal
     * coordinates wrapped in point objects, with the longitude in the X
     * component and the latitude in the Y component.
     * @param point1 the first point
     * @param point2 the second point
     * @return the distance between the two points, in meters
     */
    public static double getDistance(Point2D point1, Point2D point2) {
        return getDistance(point1.getX(), point1.getY(), point2.getX(), point2.getY());
    }

    /**
     * Compute the beeline distance between two points given in signed decimal
     * coordinates.
     * @param long1 the longitude of the first point
     * @param lat1 the latitude of the first point
     * @param long2 the longitude of the second point
     * @param lat2 the latitude of the second point
     * @return the distance between the two points, in meters
     */
    public static double getDistance(double long1, double lat1, double long2, double lat2) {

        //   make latitudes angles down from north pole
        lat1 = 90 - lat1;
        lat2 = 90 - lat2;

        //   convert degrees to radiants
        double radLong1 = ((long1 * 2 * Math.PI) / 360);
        double radLat1 = ((lat1 * 2 * Math.PI) / 360);
        double radLong2 = ((long2 * 2 * Math.PI) / 360);
        double radLat2 = ((lat2 * 2 * Math.PI) / 360);

        //   convert coordinate pairs to Cartesian system
        double x1 = Math.cos(radLong1) * Math.sin(radLat1);
        double y1 = Math.sin(radLong1) * Math.sin(radLat1);
        double z1 = Math.cos(radLat1);
        double x2 = Math.cos(radLong2) * Math.sin(radLat2);
        double y2 = Math.sin(radLong2) * Math.sin(radLat2);
        double z2 = Math.cos(radLat2);

        //   use scalar product to compute angle between vectors
        double radDist = Math.acos((x1 * x2) + (y1 * y2) + (z1 * z2));

        //   use angle to compute distance as fraction of circumference
        return ((radDist * (1000 * 1000 * 40)) / (Math.PI * 2));
    }
}

Related

  1. distance(@Nonnull Point2D pA, @Nonnull Point2D pB)
  2. distance(final double p1X, final double p1Y, final double p2X, final double p2Y)
  3. distance(Point2D a, Point2D b)
  4. DistanceSquareToLine(double x, double y, double x1, double y1, double x2, double y2)
  5. getDistance(Point2D point1, Point2D point2)
  6. getDistance(Point2D.Double pt1, Point2D.Double pt2)