Java Distance Calculate distance_to_line(double x1, double y1, double x2, double y2, double x3, double y3)

Here you can find the source of distance_to_line(double x1, double y1, double x2, double y2, double x3, double y3)

Description

Computes the distance from a point to a line segment.

License

Open Source License

Parameter

Parameter Description
x1 line x coord1
y1 line y coord1
x2 line x coord2
y2 line y coord2
x3 point x coord
y3 point y coord

Return

float distance to line segment

Declaration

public final static double distance_to_line(double x1, double y1, double x2, double y2, double x3, double y3) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from w  ww  . j  a va2 s  .c o m*/
     * Computes the distance from a point to a line segment.
     * <p>
     * Variable usage as follows:
     * <p>
     * <ul>
     * <li>x12 x distance from the first endpoint to the second.
     * <li>y12 y distance from the first endpoint to the second.
     * <li>x13 x distance from the first endpoint to point being tested.
     * <li>y13 y distance from the first endpoint to point being tested.
     * <li>x23 x distance from the second endpoint to point being tested.
     * <li>y23 y distance from the second endpoint to point being tested.
     * <li>D12 Length of the line segment.
     * <li>pp distance along the line segment to the intersection of the
     * perpendicular from the point to line extended.
     * </ul>
     * 
     * Procedure:
     * <p>
     * 
     * Compute D12, the length of the line segment. Compute pp, the distance to
     * the perpendicular. If pp is negative, the intersection is before the
     * start of the line segment, so return the distance from the start point.
     * If pp exceeds the length of the line segment, then the intersection is
     * beyond the end point so return the distance of the point from the end
     * point. Otherwise, return the absolute value of the length of the
     * perpendicular line. The sign of the length of the perpendicular line
     * indicates whether the point lies to the right or left of the line as one
     * travels from the start point to the end point.
     * <p>
     * 
     * @param x1 line x coord1
     * @param y1 line y coord1
     * @param x2 line x coord2
     * @param y2 line y coord2
     * @param x3 point x coord
     * @param y3 point y coord
     * @return float distance to line segment
     * 
     */
    public final static double distance_to_line(double x1, double y1, double x2, double y2, double x3, double y3) {

        // algorithm courtesy of Ray 1/16/98
        double x12 = x2 - x1;
        double y12 = y2 - y1;
        double x13 = x3 - x1;
        double y13 = y3 - y1;
        double D12 = Math.sqrt(x12 * x12 + y12 * y12);
        double pp = (x12 * x13 + y12 * y13) / D12;
        if (pp < 0.0) {
            return (float) Math.sqrt(x13 * x13 + y13 * y13);
        }
        if (pp > D12) {
            double x23 = x3 - x2;
            double y23 = y3 - y2;
            return Math.sqrt(x23 * x23 + y23 * y23);
        }
        return Math.abs(((x12 * y13 - y12 * x13) / D12));
    }
}

Related

  1. distance3d(double x1, double y1, double z1, double x2, double y2, double z2)
  2. distance3Sq(double x1, double y1, double z1, double x2, double y2, double z2)
  3. distance_Sphere(double rlon1, double rlat1, double rlon2, double rlat2)
  4. distance_to_endpoint(int x1, int y1, int x2, int y2, int x, int y)
  5. distance_to_endpoint(int x1, int y1, int x2, int y2, int x, int y)
  6. distanceAbs(final int a, final int b)
  7. distanceBase(double[] coord1, double[] coord2, int order)
  8. distanceBetween(double latitude1, double longitude1, double latitude2, double longitude2)
  9. distanceBetween(double lon, double lat, double otherLon, double otherLat)