【问题标题】:java code : distance between two points [duplicate]java代码:两点之间的距离[重复]
【发布时间】:2016-07-21 15:01:50
【问题描述】:

我想用下面的公式计算两点之间的距离

如何将其放入代码中以查找距离。

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

}

【问题讨论】:

    标签: java


    【解决方案1】:

    使用这个

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-15
      • 1970-01-01
      • 2014-11-14
      • 2013-01-15
      • 1970-01-01
      • 2011-12-20
      • 1970-01-01
      • 2019-01-23
      • 2012-07-17
      相关资源
      最近更新 更多