C 言語で直線の傾きを計算する

CCBeginner
今すぐ練習

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

この実験では、C 言語を使用して直線の傾きを計算する方法を学びます。この実験では主に 2 つのステップがカバーされます。まず、ユーザー入力から 2 つの点 (x1, y1) と (x2, y2) を読み取り、次に公式 (y2 - y1)/(x2 - x1) を使用して傾きを計算します。このプログラムでは、垂直線の特殊なケースも処理します。この実験の最後までに、C 言語で微積分学と解析幾何学の概念を扱う方法についてより深い理解を得ることができるでしょう。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("C")) -.-> c/BasicsGroup(["Basics"]) c(("C")) -.-> c/ControlFlowGroup(["Control Flow"]) c(("C")) -.-> c/FunctionsGroup(["Functions"]) c(("C")) -.-> c/UserInteractionGroup(["User Interaction"]) c/BasicsGroup -.-> c/variables("Variables") c/ControlFlowGroup -.-> c/if_else("If...Else") c/FunctionsGroup -.-> c/math_functions("Math Functions") c/UserInteractionGroup -.-> c/user_input("User Input") c/UserInteractionGroup -.-> c/output("Output") subgraph Lab Skills c/variables -.-> lab-435165{{"C 言語で直線の傾きを計算する"}} c/if_else -.-> lab-435165{{"C 言語で直線の傾きを計算する"}} c/math_functions -.-> lab-435165{{"C 言語で直線の傾きを計算する"}} c/user_input -.-> lab-435165{{"C 言語で直線の傾きを計算する"}} c/output -.-> lab-435165{{"C 言語で直線の傾きを計算する"}} end

2 つの点 (x1,y1) と (x2,y2) を読み取る

このステップでは、C プログラムにおいてユーザー入力から 2 つの点の座標を読み取り、直線の傾きを計算する方法を学びます。ユーザーに 2 つの点の x 座標と y 座標の入力を促す簡単なプログラムを作成します。

まず、プロジェクトディレクトリに新しい C ファイルを作成しましょう。

cd ~/project
nano slope_calculator.c

ここで、2 つの点を読み取るために次のコードを入力します。

#include <stdio.h>

int main() {
    float x1, y1, x2, y2;

    // Prompt user to enter first point coordinates
    printf("Enter the x-coordinate of the first point (x1): ");
    scanf("%f", &x1);
    printf("Enter the y-coordinate of the first point (y1): ");
    scanf("%f", &y1);

    // Prompt user to enter second point coordinates
    printf("Enter the x-coordinate of the second point (x2): ");
    scanf("%f", &x2);
    printf("Enter the y-coordinate of the second point (y2): ");
    scanf("%f", &y2);

    // Print the entered coordinates
    printf("First point: (%.2f, %.2f)\n", x1, y1);
    printf("Second point: (%.2f, %.2f)\n", x2, y2);

    return 0;
}

プログラムをコンパイルして実行します。

gcc slope_calculator.c -o slope_calculator
./slope_calculator

出力例:

Enter the x-coordinate of the first point (x1): 1
Enter the y-coordinate of the first point (y1): 2
Enter the x-coordinate of the second point (x2): 4
Enter the y-coordinate of the second point (y2): 6
First point: (1.00, 2.00)
Second point: (4.00, 6.00)

傾き = (y2 - y1)/(x2 - x1) を計算する

このステップでは、前のプログラムを修正して、点斜式を使用して直線の傾きを計算します。傾きの計算を追加し、垂直線の特殊なケースを処理します。

前のファイルを開き、コードを更新します。

cd ~/project
nano slope_calculator.c

内容を次のコードに置き換えます。

#include <stdio.h>

int main() {
    float x1, y1, x2, y2, slope;

    // Prompt user to enter first point coordinates
    printf("Enter the x-coordinate of the first point (x1): ");
    scanf("%f", &x1);
    printf("Enter the y-coordinate of the first point (y1): ");
    scanf("%f", &y1);

    // Prompt user to enter second point coordinates
    printf("Enter the x-coordinate of the second point (x2): ");
    scanf("%f", &x2);
    printf("Enter the y-coordinate of the second point (y2): ");
    scanf("%f", &y2);

    // Check for vertical line (undefined slope)
    if (x2 == x1) {
        printf("Slope is undefined (vertical line)\n");
        return 0;
    }

    // Calculate slope
    slope = (y2 - y1) / (x2 - x1);

    // Print the results
    printf("First point: (%.2f, %.2f)\n", x1, y1);
    printf("Second point: (%.2f, %.2f)\n", x2, y2);
    printf("Slope: %.2f\n", slope);

    return 0;
}

プログラムをコンパイルして実行します。

gcc slope_calculator.c -o slope_calculator
./slope_calculator

出力例:

Enter the x-coordinate of the first point (x1): 1
Enter the y-coordinate of the first point (y1): 2
Enter the x-coordinate of the second point (x2): 4
Enter the y-coordinate of the second point (y2): 6
First point: (1.00, 2.00)
Second point: (4.00, 6.00)
Slope: 1.33

傾きを表示する

この最後のステップでは、傾きの計算プログラムを拡張し、より詳細な出力を追加し、傾きの表示を整形します。明確で有益な傾き情報を提供することで、ユーザー体験を向上させます。

前のファイルを開き、コードを更新します。

cd ~/project
nano slope_calculator.c

内容を次のコードに置き換えます。

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

int main() {
    float x1, y1, x2, y2, slope;

    // Prompt user to enter first point coordinates
    printf("Slope Calculator\n");
    printf("================\n");
    printf("Enter the x-coordinate of the first point (x1): ");
    scanf("%f", &x1);
    printf("Enter the y-coordinate of the first point (y1): ");
    scanf("%f", &y1);

    // Prompt user to enter second point coordinates
    printf("Enter the x-coordinate of the second point (x2): ");
    scanf("%f", &x2);
    printf("Enter the y-coordinate of the second point (y2): ");
    scanf("%f", &y2);

    // Check for vertical line (undefined slope)
    if (x2 == x1) {
        printf("\nResult:\n");
        printf("First point:  (%.2f, %.2f)\n", x1, y1);
        printf("Second point: (%.2f, %.2f)\n", x2, y2);
        printf("Slope: Undefined (Vertical Line)\n");
        return 0;
    }

    // Calculate slope
    slope = (y2 - y1) / (x2 - x1);

    // Print detailed results
    printf("\nResult:\n");
    printf("First point:  (%.2f, %.2f)\n", x1, y1);
    printf("Second point: (%.2f, %.2f)\n", x2, y2);
    printf("Slope Calculation: (%.2f - %.2f) / (%.2f - %.2f) = %.2f\n",
           y2, y1, x2, x1, slope);

    // Additional slope interpretation
    if (slope > 0) {
        printf("Slope Interpretation: Positive slope (line rises from left to right)\n");
    } else if (slope < 0) {
        printf("Slope Interpretation: Negative slope (line falls from left to right)\n");
    } else {
        printf("Slope Interpretation: Horizontal line (zero slope)\n");
    }

    return 0;
}

プログラムをコンパイルして実行します。

gcc slope_calculator.c -o slope_calculator
./slope_calculator

出力例:

Slope Calculator
================
Enter the x-coordinate of the first point (x1): 1
Enter the y-coordinate of the first point (y1): 2
Enter the x-coordinate of the second point (x2): 4
Enter the y-coordinate of the second point (y2): 6

Result:
First point:  (1.00, 2.00)
Second point: (4.00, 6.00)
Slope Calculation: (6.00 - 2.00) / (4.00 - 1.00) = 1.33
Slope Interpretation: Positive slope (line rises from left to right)

まとめ

この実験では、C プログラムにおいてユーザー入力から 2 つの点の座標を読み取り、点斜式を使用して直線の傾きを計算する方法を学びます。このプログラムは、ユーザーに 2 つの点の x 座標と y 座標の入力を促し、その後直線の傾きを計算して表示します。さらに、傾きが定義されない垂直線の特殊なケースも処理します。

この実験では以下の主要なステップがカバーされます。1) ユーザー入力から 2 つの点 (x1, y1) と (x2, y2) を読み取り、2) 公式 (y2 - y1) / (x2 - x1) を使用して傾きを計算します。その後、プログラムは計算された傾きを表示します。