-2

I want to use the following formula to calculate distance between two points

enter image description here

How do put this into code to find the distance.

public class Point
{
    public int x; // the x coordinate
    public int y; // the y coordinate

    public Point (int x, int y)
    {
        this.x=x; 
        this.y=y;
    }

    public static double distance (Point p1, Point p2)
    {
         // to do
        return 0.0;
    }

    public String toString()
    {
        return "("+x+","+y+")";
    }

}
Codebender
  • 14,221
  • 7
  • 48
  • 85
YChoi
  • 3
  • 1
  • 5

1 Answers1

4

Use this

public static double distance (Point p1, Point p2)
{
    double dist = Math.sqrt(Math.pow(p1.x - p2.x, 2.0) + Math.pow(p1.y - p2.y, 2.0));
    return dist;
}
CloudPotato
  • 1,255
  • 1
  • 17
  • 32