java編程座標距離問題(二維)

package testmypoint;
class MyPoint{
    private double x;
    private double y;
    public MyPoint()//默認值
    {
        x=0;
        y=0;
    }
    public MyPoint(double x,double y)
    {
        this.x=x;
        this.y=y;
    }
    public double distance(MyPoint m)//給一個點
        {
        double a=Math.sqrt((this.x-m.x)*(this.x-m.x)+(this.y-m.y)*(this.y-m.y));
        return a;
    }
    public double distance(double x,double y)//給出座標
        {
        double a=Math.sqrt((this.x-x)*(this.x-x)+(this.y-y)*(this.y-y));
        return a;
    }
}
public class TestMyPoint //測試類((10,30.5)到(0,0)的距離)
{
    public static void main(String[] args) {
        MyPoint a1=new MyPoint();
        MyPoint a2=new MyPoint(10,30.5);
        System.out.printf("%.2f\n",a1.distance(a2));
        System.out.printf("%.2f\n",a1.distance(10, 30.5));
    } 
}
相關文章
相關標籤/搜索