C로 구현한 직선 보간 (Linear Interpolation) 알고리즘

다음은 직선 보간 알고리즘을 C 코드로 구현한 예제입니다. 이 코드는 두 점 `(x0, y0)`와 `(x1, y1)` 사이를 직선으로 연결하면서 보간하여 중간 점들을 계산합니다.

이 코드는 다음과 같이 작동합니다:

1. `dx`와 `dy`를 계산하여 두 점 사이의 거리 차이를 구합니다.
2. 가장 큰 거리(`dx` 또는 `dy`)를 `steps`로 설정하여 보간 단계의 개수를 결정합니다.
3. 각 단계의 `x`와 `y` 변화량(`x_increment`, `y_increment`)을 계산합니다.
4. 루프를 사용하여 `steps`만큼 점을 생성하고, 각 점을 출력합니다.

이 코드를 실행하면 입력된 두 점 사이의 모든 중간 점을 계산하고 출력합니다.

#include <stdio.h>
#include <math.h>

// Function to perform linear interpolation
void linear_interpolation(int x0, int y0, int x1, int y1) {
    int dx = abs(x1 - x0);
    int dy = abs(y1 - y0);
    
    int steps = (dx > dy) ? dx : dy; // Choose the larger of dx or dy

    float x_increment = (float)(x1 - x0) / steps;
    float y_increment = (float)(y1 - y0) / steps;

    float x = x0;
    float y = y0;

    printf("Interpolated points:\n");
    for (int i = 0; i <= steps; i++) {
        printf("(%.2f, %.2f)\n", x, y);
        x += x_increment;
        y += y_increment;
    }
}

int main() {
    int x0, y0, x1, y1;

    // Input points from the user
    printf("Enter the coordinates of the first point (x0, y0): ");
    scanf("%d %d", &x0, &y0);

    printf("Enter the coordinates of the second point (x1, y1): ");
    scanf("%d %d", &x1, &y1);

    // Call the interpolation function
    linear_interpolation(x0, y0, x1, y1);

    return 0;
}