Here you can find the source of distance(float x1, float y1, float x2, float y2)
Parameter | Description |
---|---|
x1 | x coord |
y1 | y coord |
x2 | x coord |
y2 | y coord |
public final static float distance(float x1, float y1, float x2, float y2)
//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)); } }