본문 바로가기

IT프로그래밍

c언어 두 점 사이의 거리 / 두 점 사이의 거리 구하는 c언어 알고리즘

두 점 사이의 거리를 구하는 C언어 프로그램


두 점 사이의 거리를 구하는 c언어 소스

#include <stdio.h>

#include <math.h>      //sqrt(제곱근) 위한 math.h헤더 추가

/*

        사이의 거리를 구하는 프로그램

*/

main()

{

        int spot_1[2], spot_2[2];

        float distance;

       

        printf("_1좌표를 입력 해주세요(x y) : ");

        scanf_s("%d %d", &spot_1[0], &spot_1[1]);

       

 

        printf("_2좌표를 입력 해주세요(x y) : ");

        scanf_s("%d %d", &spot_2[0], &spot_2[1]);

       

 

        /*

        1 2 사이의 거리 구하는 공식

        root((1_x - 2_x)^2+(1_y - 2_y)^2)

        pow = 제곱을 해주는 math 함수 : pow(제곱하고자 하는수, 몇번 제곱 )

        ex) pow(5, 2) = 25 = 5*5

               pow(2, 4) = 16 = 2*2*2*2

        sqrt = 제곱근을 구하는 math 함수 : sqrt(제곱근을 구할 )

        ex) sqrt(100) = 10

               sqrt(4) = 2

        */

        distance = sqrt(pow((spot_1[0] - spot_2[0]), 2) + pow((spot_1[1] - spot_2[1]), 2));

        printf("_1(%d, %d)에서 _2(%d, %d)까지의 거리 : %.3f\n", spot_1[0], spot_1[1], spot_2[0], spot_2[1], distance);  //%.3f float형으로 소수점 3번째 자리까지 표시

}


두 점 사이의 거리를 구하는 c언어 결과






두 점 사이의 거리를 구하는 c언어 알고리즘