distance between two Point2D object - Java 2D Graphics

Java examples for 2D Graphics:Point

Description

distance between two Point2D object

Demo Code


//package com.java2s;
import java.awt.geom.Point2D;

public class Main {
    /**// ww w  .  jav a 2 s . com
     * @param source
     * @param listener
     * @return
     */
    public static double distance(Point2D source, Point2D listener) {
        return Math.sqrt(distanceSqrd(source, listener));
    }

    /**
     * @param source
     * @param listener
     * @return
     */
    public static double distanceSqrd(Point2D source, Point2D listener) {
        return (source.getX() - listener.getX())
                * (source.getX() - listener.getX())
                + (source.getY() - listener.getY())
                * (source.getY() - listener.getY());
    }
}

Related Tutorials