编程语言
首页 > 编程语言> > java编程坐标距离问题(二维)

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

标签:distance,java,10,double,编程,30.5,二维,MyPoint,public
来源: https://blog.51cto.com/14233078/2407562