java代码:两点之间的距离

时间:2016-07-21 06:54:00

标签: java

我想使用以下公式来计算两点之间的距离

enter image description here

如何将此代码放入代码中以找到距离。

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+")";
    }

}

1 个答案:

答案 0 :(得分:3)

使用此

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;
}
相关问题