Java에서 두 점 사이의 거리 계산
Sheeraz Gul
2023년10월12일
Java
Java Math

피타고라스 정리를 사용하여 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
튜토리얼이 마음에 드시나요? DelftStack을 구독하세요 YouTube에서 저희가 더 많은 고품질 비디오 가이드를 제작할 수 있도록 지원해주세요. 구독하다
작가: Sheeraz Gul
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn Facebook