Java Distance Calculate distance(float x1, float y1, float x2, float y2)

Here you can find the source of distance(float x1, float y1, float x2, float y2)

Description

2D distance formula.

License

Open Source License

Parameter

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

Return

float distance

Declaration

public final static float distance(float x1, float y1, float x2, float y2) 

Method Source Code

//package com.java2s;

public class Main {
    /**//  ww  w . j a  v a  2  s . c  o  m
     * 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. distance(float speed, float delta)
  2. distance(float value1, float value2)
  3. distance(float value1, float value2)
  4. distance(float value1, float value2)
  5. distance(float x1, float x2, float y1, float y2)
  6. distance(float x1, float y1, float x2, float y2)
  7. distance(float x1, float y1, float x2, float y2)
  8. distance(float[] p1, float[] p2)
  9. distance(float[] values1, float[] values2)