Two dimensional euclidean distance. - Java java.lang

Java examples for java.lang:Math Trigonometric Function

Description

Two dimensional euclidean distance.

Demo Code


//package com.java2s;

public class Main {
    /**/*from ww w.ja v a  2s .c o  m*/
     * Two dimensional euclidean distance.
     * @param x1 X value 1.
     * @param y1 Y value 1.
     * @param x2 X value 2.
     * @param y2 Y value 2.
     * @return The distance between the two values.
     */
    public static double EuclideanDistance(int x1, double y1, int x2,
            double y2) {
        double res = 0;
        res += Math.pow(x1 - x2, 2);
        res += Math.pow(y1 - y2, 2);
        res = Math.sqrt(res);
        return res;
    }

    /**
     * One dimensional euclidean distance.
     * @param a Value A.
     * @param b Value B.
     * @return The distance between the two values.
     */
    public static double EuclideanDistance(double a, double b) {
        double res;
        res = Math.pow(a - b, 2);
        res = Math.sqrt(res);
        return res;
    }
}

Related Tutorials