Java Distance Calculate distance_to_endpoint(int x1, int y1, int x2, int y2, int x, int y)

Here you can find the source of distance_to_endpoint(int x1, int y1, int x2, int y2, int x, int y)

Description

Distance to closest endpoint.

License

Open Source License

Parameter

Parameter Description
x1 x coord
y1 y coord
x2 x coord
y2 y coord
x x coord of point
y y coord of point

Return

float distance to endpoint

Declaration

public final static float distance_to_endpoint(int x1, int y1, int x2, int y2, int x, int y) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from  w  ww .  j a  va2s  .c o m*/
     * Distance to closest endpoint.
     * <p>
     * 
     * @param x1 x coord
     * @param y1 y coord
     * @param x2 x coord
     * @param y2 y coord
     * @param x x coord of point
     * @param y y coord of point
     * @return float distance to endpoint
     */
    public final static float distance_to_endpoint(int x1, int y1, int x2, int y2, int x, int y) {

        return (float) Math.min(distance(x1, y1, x, y), distance(x2, y2, x, y));
    }

    /**
     * 2D distance formula.
     * <p>
     * 
     * @param x1 x coord
     * @param y1 y coord
     * @param x2 x coord
     * @param y2 y coord
     * @return float distance
     */
    public final static float distance(float x1, float y1, float x2, float y2) {
        double xdiff = x2 - x1;
        double ydiff = y2 - y1;
        return (float) Math.sqrt((xdiff * xdiff + ydiff * ydiff));
    }

    /**
     * 2D distance formula.
     * <p>
     * 
     * @param x1 x coord
     * @param y1 y coord
     * @param x2 x coord
     * @param y2 y coord
     * @return float distance
     */
    public final static float distance(int x1, int y1, int x2, int y2) {
        double xdiff = x2 - x1;
        double ydiff = y2 - y1;
        return (float) Math.sqrt((xdiff * xdiff + ydiff * ydiff));
    }

    /**
     * 2D distance formula.
     * <p>
     * 
     * @param x1 x coord
     * @param y1 y coord
     * @param x2 x coord
     * @param y2 y coord
     * @return double distance
     */
    public final static double distance(double x1, double y1, double x2, double y2) {
        double xdiff = x2 - x1;
        double ydiff = y2 - y1;
        return Math.sqrt((xdiff * xdiff + ydiff * ydiff));
    }
}

Related

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