Point类,具有改变x,y,z坐标,计算该点到原点距离,计算该点到其他点的距离的方法。(全是int型)
public class Point { private int x; private int y; private int z; public Point(int _x,int _y,int _z) { x = _x; y = _y; z = _z; } public void setx(int _x) { x = _x; } public void sety(int _y) { y = _y; } public void setz(int _z) { z = _z; } public int distanceOrigin() { //计算该点到原点的距离 int dis; dis = x * x + y * y + z * z; return dis; } public int distanceOther(Point p) { //计算该点到另一个点的距离 int dis = (x - p.x)*(x - p.x) + (y - p.y)*(y - p.y) + (z - p.z) * (z - p.z); return dis; } }
测试
public class Test { public static void main(String[] args) { int x = 20,y = 30,z = 40; Point p1 = new Point(2,3,4); Point p2 = new Point(2,3,4); System.out.println(p1.distanceOrigin()); p1.setx(x); p1.sety(y); p1.setz(z); System.out.println(p1.distanceOrigin()); System.out.print("p1与p2的距离:" + p1.distanceOther(p2)); } }
原文地址:https://www.cnblogs.com/guua/p/12416640.html
时间: 2024-10-14 15:52:50