C Program to Check if Three Points Form a Triangle

C Program to Check if Three Points Form a Triangle

C Program to Check if Three Points Form a Triangle

Introduction

In geometry, three points in a 2D plane can form a triangle if they are non-collinear. This means the three points do not lie on a single straight line. A common way to determine this is by using the area of the triangle formed by the three points. If the area is zero, the points are collinear; otherwise, they form a triangle.

This program demonstrates how to check if three points form a triangle using the area formula. It provides a clear implementation in C, suitable for beginners and experienced programmers alike.


Formula

The area of a triangle formed by three points, (x1, y1), (x2, y2), and (x3, y3), can be calculated using the formula:

Area = 0.5 * |x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2)|

To determine if the points form a triangle:

  • Calculate the area using the formula above.
  • If the area is 0, the points are collinear and do not form a triangle.
  • If the area is greater than 0, the points form a triangle.

Code

The following is the C program to check if three points form a triangle:

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

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

    // Input coordinates of the three points
    printf("Enter coordinates of point 1 (x1, y1): ");
    scanf("%f %f", &x1, &y1);

    printf("Enter coordinates of point 2 (x2, y2): ");
    scanf("%f %f", &x2, &y2);

    printf("Enter coordinates of point 3 (x3, y3): ");
    scanf("%f %f", &x3, &y3);

    // Calculate the area of the triangle
    area = fabs(0.5 * (x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2)));

    // Check if the area is zero
    if (area == 0) {
        printf("The points are collinear and do not form a triangle.\\n");
    } else {
        printf("The points form a triangle.\\n");
    }

    return 0;
}
        

Output

Here is an example of how the program runs:

Enter coordinates of point 1 (x1, y1): 1 1
Enter coordinates of point 2 (x2, y2): 4 4
Enter coordinates of point 3 (x3, y3): 6 6
The points are collinear and do not form a triangle.
        

In this example, the points (1, 1), (4, 4), and (6, 6) are collinear, meaning they lie on the same straight line and cannot form a triangle.

Another example:

Enter coordinates of point 1 (x1, y1): 0 0
Enter coordinates of point 2 (x2, y2): 4 0
Enter coordinates of point 3 (x3, y3): 2 3
The points form a triangle.
        

Here, the points (0, 0), (4, 0), and (2, 3) are non-collinear, so they form a triangle.


Explanation

The program follows these steps:

  • Input: The user is prompted to enter the coordinates of three points. These values are read using the scanf function.
  • Calculation:
    • The formula for the area of the triangle is applied. The fabs function from the math.h library is used to get the absolute value of the area.
    • The area is calculated as a floating-point value to handle all possible inputs accurately.
  • Decision:
    • If the calculated area is 0, the program outputs that the points are collinear and cannot form a triangle.
    • Otherwise, it confirms that the points form a triangle.

The use of the area formula ensures an efficient and reliable way to determine whether three points form a triangle. This method is widely used in computational geometry and is a fundamental concept in mathematics and computer programming.