HOWTO · Java
Java에서 두 점 사이의 거리 계산
이 튜토리얼은 Java에서 두 점 사이의 거리를 계산하는 방법을 보여줍니다.
이 페이지의 내용
피타고라스 정리를 사용하여 Java에서 두 점 사이의 거리를 찾을 수 있습니다. 이 튜토리얼은 Java에서 두 점 사이의 거리를 계산하는 방법을 보여줍니다.
Java에서 두 점 사이의 거리 계산
예를 들어 두 점 X와 Y는 좌표 (x1, y1)와 (x2, y2)를 가지며 이 두 점 사이의 거리는 XY로 표시될 수 있으며 피타고라스 정리 거리를 계산하기 위해 Java로 구현할 수 있습니다.
아래 그림의 방정식은 이 두 점에 대한 피타고라스 정리를 나타냅니다.
자바에서 피타고라스 정리를 구현해 봅시다.
package delftstack;
import java.util.Scanner;
public class Distance_Two_Points {
public static void main(String[] args) {
Scanner Temp = new Scanner(System.in);
// declare the variables
int x1;
int x2;
int y1;
int y2;
int x;
int y;
double Distance_Result;
// get the input coordinates
System.out.print("Enter the values of first point coordinates : ");
x1 = Temp.nextInt();
y1 = Temp.nextInt();
System.out.print("Enter the values of second point coordinates : ");
x2 = Temp.nextInt();
y2 = Temp.nextInt();
// Implement pythagorean theorem
x = x2 - x1;
y = y2 - y1;
Distance_Result = Math.sqrt(x * x + y * y);
System.out.println("Distance between the two points is : " + Distance_Result);
}
}
위의 코드는 두 점의 좌표를 취한 다음 피타고라스 정리로 거리를 계산합니다. 출력 참조:
Enter the values of first point coordinates : 12
21
Enter the values of second point coordinates : 13
34
Distance between the two points is : 13.038404810405298