📝 Coding Test Study/Math

[Math&Algorithm][C++] 유클리디안 거리(Euclidean Distance)

ibelieveinme 2021. 8. 9. 03:02
728x90

두 점 사이의 거리를 구할 때 사용하는 '유클리디안 거리'공식 !

 

두 점 (x1, y1), (x2, y2)이 주어졌을 때, 두 점 사이의 거리는 다음 공식으로 구할 수 있다.

#include <iostream>
#include <cmath> // sqrt()와 pow() 함수를 사용하기 위해

using namespace std;

int main(){
	int x1, y1, x2, y2;
    double distance;
    
    cin >> x1 >> y1 >> x2 >> x2;
    distance = sqrt(pow(x2-x1,2) + pow(y2 - y1, 2));
    cout << distance;
    
	return 0;
}

 

① pow(): n 제곱을 구하기

② sqrt(): 루트 구하기

728x90