C Func hypot - sonkoni/Koni-Wiki GitHub Wiki

math.h | tgmath.h

hypot, hypotf, hypotl

삼각형 빗변 계산(√(x² + y²))

float       hypotf(float x, float y);
double      hypot(double x, double y);
long double hypotl(long double x, long double y);

#define     hypot(x, y)   // tgmath.h

// x, y  : 좌표
// 실수 루트(√x)를 반환. 만약 arg 가 음수이면 NaN을 반환.p2(x, y)
    ╱│
   ╱ │     직선 p1-p2  길이 = √(x² + y²)
  ╱  │
 •───┘
p1(x, y)

피타고라스 정리를 통해 나오는 빗변을 한방에 계산한다.

int a = p2.x - p1.x;
int b = p2.y - p1.y;
hypot(a, b) == sqrt(pow(a, 2) + pow(b, 2))
#include <stdio.h>
#include <math.h>
int main(int argc, char *argv[]) {
    
    // p1(30, 20)
    // p2(60, 50)
    int a = 60 - 30;
    int b = 50 - 20;
    double c = sqrt(pow(a, 2) + pow(b, 2));
    printf("=> %f, %f", c, hypot(a, b));
    
    return 0;
}
// => 42.426407, 42.426407
⚠️ **GitHub.com Fallback** ⚠️