java-计算两点之间的距离


java-计算两点之间的距离_第1张图片


package com.qyx;

import java.util.Arrays;

class point {
	double x;
	double y;
	double z;
	
	point(double _x,double _y,double _z)
	{
		x=_x;
		y=_y;
		z=_z;
	}
	
	void setX(double _x){
		x=_x;
	}
	
	void setY(double _y){
		y=_y;
	}
	
	void setZ(double _z){
		z=_z;
	}
	
	double getDistance(point P){
		return (x-P.x)*(x-P.x)+(y-P.y)*(y-P.y)+(z-P.z)*(z-P.z);
	}
}

public class pro2 {
	public static void main(String[] args){
		point p=new point(1.0, 2.0, 3.0);
		point p1=new point(0.0, 0.0, 0.0);
		System.out.println(p.getDistance(p1));
		
		p.setX(5.0);
		System.out.println(p.getDistance(new point(1.0,1.0,1.0)));
	}
	
}


你可能感兴趣的:(java)