Computer >> 컴퓨터 >  >> 프로그램 작성 >> C 프로그래밍

두 점 사이의 거리를 계산하는 C 프로그램

<시간/>

두 점 좌표가 주어지면 두 점 사이의 거리를 찾아 결과를 표시하는 작업입니다.

2차원 평면에 A와 B가 있고 각각의 좌표가 (x1, y1) 및 (x2, y2)인 두 점이 있고 그 사이의 거리를 계산하기 위해 아래에 주어진 직접적인 공식이 있습니다.

$$\sqrt{\l그룹 x2-x1\rgroup^{2}+\l그룹 y2-y1\rgroup^{2}}$$

아래는 두 점과 그 차이점을 나타내는 다이어그램입니다.

$$\frac{(x_2-x_1)}{(x_1,y_1)\:\:\:\:\:\:(y_2-y_1)\:\:\:\:\:\:(x_2,y_2 )}$$

아래에 사용된 접근 방식은 다음과 같습니다. -

  • 좌표를 x1, x2, y1, y2로 입력
  • 수식을 적용하여 두 점의 차이 계산
  • 거리 인쇄

알고리즘

Start
Step 1-> declare function to calculate distance between two point
   void three_dis(float x1, float y1, float x2, float y2)
      set float dis = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0)
      print dis
step 2-> In main()
   Set float x1 = 4
   Set float y1 = 9
   Set float x2 = 5
   Set float y2 = 10
   Call two_dis(x1, y1, x2, y2)
Stop

예시

#include <stdio.h>
#include<math.h>
//function to find distance bewteen 2 points
void two_dis(float x1, float y1, float x2, float y2) {
   float dis = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0);
   printf("Distance between 2 points are : %f", dis);
   return;
}
int main() {
   float x1 = 4;
   float y1 = 9;
   float x2 = 5;
   float y2 = 10;
   two_dis(x1, y1, x2, y2);
   return 0;
}

출력

위의 코드를 실행하면 다음 출력이 생성됩니다.

Distance between 2 points are : 1.414214