JAVA/Java SE
[JAVA] 두 점 사이의 거리 구하기
developekkyu37
2022. 12. 21. 16:27
728x90
MyPoint.java
public class MyPoint {
public int x;
public int y;
MyPoint(int x, int y){
this.x = x;
this.y = y;
}
public double getDistance(int i, int j) {
double dis;
dis = Math.sqrt((this.x-i)*(this.x-i) + (this.y-j)*(this.y-j));
return dis;
}
}
MyPointExam.java
public class MyPointExam {
public static void main(String[] args) {
// System.out.println(Math.sqrt(4)); // 2.0*2.0 == 4
// System.out.println(Math.sqrt(9)); // 3.0*3.0 ==9
MyPoint p = new MyPoint(1,1);
double dis = p.getDistance(1, 5);
System.out.printf("두 점 사이의 거리는 = %4.0f\n", dis);
}
}
--
두 점 사이의 거리는 = 4
728x90