Find the Equation of a Line Given Two Points in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to find the equation of a line given two points in C programming. The lab covers three main steps: computing the slope from two points, calculating the y-intercept using the slope and a known point, and finally printing the complete line equation in the form y = mx + b. By the end of this lab, you will have a solid understanding of how to work with lines in analytical geometry using C.

The lab starts by demonstrating how to calculate the slope between two points using the formula (y2 - y1) / (x2 - x1). It then shows how to determine the y-intercept by rearranging the equation y = mx + b to solve for the constant b. Finally, the complete line equation is printed, allowing you to represent the line in the standard form.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/operators("`Operators`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435182{{"`Find the Equation of a Line Given Two Points in C`"}} c/variables -.-> lab-435182{{"`Find the Equation of a Line Given Two Points in C`"}} c/operators -.-> lab-435182{{"`Find the Equation of a Line Given Two Points in C`"}} c/math_functions -.-> lab-435182{{"`Find the Equation of a Line Given Two Points in C`"}} end

Compute Slope from Two Points

In this step, you will learn how to compute the slope between two points in a C program. The slope represents the steepness of a line and is calculated using the formula: slope = (y2 - y1) / (x2 - x1).

First, create a new C file to implement the slope calculation:

cd ~/project
nano slope_calculation.c

Now, write the following C code to calculate the slope:

#include <stdio.h>

float calculate_slope(float x1, float y1, float x2, float y2) {
    // Check if points are on the same vertical line
    if (x2 - x1 == 0) {
        printf("Undefined slope (vertical line)\n");
        return 0;
    }

    // Calculate slope using the formula: (y2 - y1) / (x2 - x1)
    float slope = (y2 - y1) / (x2 - x1);
    return slope;
}

int main() {
    float x1 = 2.0, y1 = 3.0;  // First point
    float x2 = 5.0, y2 = 7.0;  // Second point

    float slope = calculate_slope(x1, y1, x2, y2);

    printf("Point 1: (%.1f, %.1f)\n", x1, y1);
    printf("Point 2: (%.1f, %.1f)\n", x2, y2);
    printf("Slope: %.2f\n", slope);

    return 0;
}

Compile and run the program:

gcc slope_calculation.c -o slope_calculation
./slope_calculation

Example output:

Point 1: (2.0, 3.0)
Point 2: (5.0, 7.0)
Slope: 1.33

Let's break down the code:

  1. The calculate_slope() function takes four parameters: x1, y1, x2, y2
  2. It first checks if the points create a vertical line (x2 - x1 = 0)
  3. If not a vertical line, it calculates the slope using the standard formula
  4. The main() function demonstrates how to use the slope calculation

The slope represents the change in y-coordinate divided by the change in x-coordinate between two points. In this example, the slope is approximately 1.33, which means for every 1 unit change in x, y changes by 1.33 units.

Compute Intercept Using y - mx

In this step, you will learn how to compute the y-intercept of a line using the slope and a known point. The y-intercept is the point where the line crosses the y-axis, and it can be calculated using the equation: b = y - mx.

Continue working in the same project directory and modify the previous C file:

cd ~/project
nano line_equation.c

Write the following C code to calculate the y-intercept:

#include <stdio.h>

float calculate_slope(float x1, float y1, float x2, float y2) {
    if (x2 - x1 == 0) {
        printf("Undefined slope (vertical line)\n");
        return 0;
    }
    return (y2 - y1) / (x2 - x1);
}

float calculate_intercept(float x, float y, float slope) {
    // Calculate y-intercept using the formula: b = y - mx
    float intercept = y - (slope * x);
    return intercept;
}

int main() {
    float x1 = 2.0, y1 = 3.0;  // First point
    float x2 = 5.0, y2 = 7.0;  // Second point

    float slope = calculate_slope(x1, y1, x2, y2);
    float intercept = calculate_intercept(x1, y1, slope);

    printf("Point 1: (%.1f, %.1f)\n", x1, y1);
    printf("Point 2: (%.1f, %.1f)\n", x2, y2);
    printf("Slope: %.2f\n", slope);
    printf("Y-intercept: %.2f\n", intercept);

    return 0;
}

Compile and run the program:

gcc line_equation.c -o line_equation
./line_equation

Example output:

Point 1: (2.0, 3.0)
Point 2: (5.0, 7.0)
Slope: 1.33
Y-intercept: 0.33

Let's break down the new code:

  1. The calculate_intercept() function takes three parameters: x, y, and slope
  2. It uses the formula b = y - mx to calculate the y-intercept
  3. In the main() function, we use the previously calculated slope and a point to find the y-intercept
  4. The y-intercept represents where the line crosses the y-axis when x = 0

The calculation shows that for the given points, the line has a slope of 1.33 and a y-intercept of 0.33.

Print Line Equation y = mx + b

In this step, you will learn how to print the complete line equation using the slope and y-intercept calculated in the previous steps. We'll modify the existing C program to display the line equation in the standard form y = mx + b.

Continue working in the same project directory:

cd ~/project
nano line_equation_final.c

Write the following C code to print the line equation:

#include <stdio.h>

float calculate_slope(float x1, float y1, float x2, float y2) {
    if (x2 - x1 == 0) {
        printf("Undefined slope (vertical line)\n");
        return 0;
    }
    return (y2 - y1) / (x2 - x1);
}

float calculate_intercept(float x, float y, float slope) {
    return y - (slope * x);
}

void print_line_equation(float slope, float intercept) {
    printf("Line Equation: y = ");

    // Print slope coefficient
    if (slope == 1) {
        printf("x");
    } else if (slope == -1) {
        printf("-x");
    } else if (slope != 0) {
        printf("%.2fx", slope);
    }

    // Print intercept
    if (intercept > 0) {
        printf(" + %.2f", intercept);
    } else if (intercept < 0) {
        printf(" - %.2f", -intercept);
    }

    printf("\n");
}

int main() {
    float x1 = 2.0, y1 = 3.0;  // First point
    float x2 = 5.0, y2 = 7.0;  // Second point

    float slope = calculate_slope(x1, y1, x2, y2);
    float intercept = calculate_intercept(x1, y1, slope);

    printf("Point 1: (%.1f, %.1f)\n", x1, y1);
    printf("Point 2: (%.1f, %.1f)\n", x2, y2);
    printf("Slope: %.2f\n", slope);
    printf("Y-intercept: %.2f\n", intercept);

    print_line_equation(slope, intercept);

    return 0;
}

Compile and run the program:

gcc line_equation_final.c -o line_equation_final
./line_equation_final

Example output:

Point 1: (2.0, 3.0)
Point 2: (5.0, 7.0)
Slope: 1.33
Y-intercept: 0.33
Line Equation: y = 1.33x + 0.33

Let's break down the new code:

  1. The print_line_equation() function handles different cases of slope and intercept
  2. It handles special cases like slope of 1, -1, or 0
  3. It formats the equation with proper signs for the intercept
  4. The function provides a clean, readable representation of the line equation

The code demonstrates how to convert point and slope information into a standard linear equation format.

Summary

In this lab, you first learned how to compute the slope between two points using the formula (y2 - y1) / (x2 - x1). This represents the steepness of the line. Then, you learned how to calculate the y-intercept of the line using the slope and a known point, by rearranging the equation y = mx + b to solve for b. Finally, you combined the slope and intercept to print the complete equation of the line in the form y = mx + b.

Other C Tutorials you may like