Example 44: Area of a Triangle
Learn how to calculate the area of a triangle using pointers.
We'll cover the following...
Problem
Write a function to compute the distance between two points, and use it to develop another function that will compute the area of the triangle whose vertices are A (x1, y1), B (x2, y2), and C (x3, y3), and ascertain if the triangle is valid.
Example
| Input (Point 1, Point 2, Point 3) | Output |
|---|---|
| 0 0 6 0 0 8 |
Length of first side = 6.000000 Length of second side = 10.000000 Length of third side = 8.000000 Area: 24.000000 |
| 0 4 0 8 0 15 |
Length of first side = 4.000000 Length of second side = 7.000000 Length of third side = 11.000000 Not a triangle |
Try it yourself
Try to solve this question on your own in the code widget below. If you get stuck, you can always refer to the solution provided.
❗Note: To run your solution or our given solution, follow the following steps:
- Click on the run button.
- Wait for the 4 steps to complete, and then switch to the terminal tab.
- In the terminal tab, write the following command:
gcc main.c -o main -lm
Then, write this command:
./main
❗Note: The C language requires -lm flag while executing a program that uses the math library.
/* Calculate area of triangle */
#include <stdio.h>
#include <math.h>
int isTriangleValid ( float s1, float s2, float s3 ){
//determine if the given sides form a valid triangle
}
float areaOfTriangle (float s1, float s2, float s3) {
//calculate the area of a triangle
}
float distance (int x1, int y1, int x2, int y2) {
//calculate the length of the sides
}
int main() {
int x1, y1, x2, y2, x3, y3;
float s1, s2, s3, area;
int isValid = 0;
printf ("Enter the coordinates of 3 vertices of the triangle: \n" ) ;
printf ("First Vertex (x, y): \n" );
scanf ("%d %d", &x1, &y1);
printf ("Second Vertex (x, y): \n");
scanf ("%d %d", &x2, &y2);
printf ("Third Vertex (x, y): \n");
scanf ("%d %d", &x3, &y3);
s1 = distance (x1, y1, x2, y2);
s2 = distance (x2, y2, x3, y3);
s3 = distance (x1, y1, x3, y3);
printf ("Length of first side = %f\n", s1);
printf ("Length of second side = %f\n", s2);
printf ("Length of third side = %f\n", s3);
isValid = isTriangleValid (s1, s2, s3);
if (isValid) {
area = areaOfTriangle (s1, s2, s3);
printf ("Area: %f\n", area);
}
else
printf ("Not a triangle. \n");
return 0;
}Solution
Given below is our recommended way to approach this problem.
/* Calculate area of triangle */
#include <stdio.h>
#include <math.h>
int isTriangleValid ( float s1, float s2, float s3 ){
if (s1 <= 0 || s2 <= 0 || s3 <= 0)
return 0;
else if ((s1 + s2 <= s3 ) || (s2 + s3 <= s1) || (s1 + s3 <= s2))
return 0;
else
return 1;
}
float areaOfTriangle (float s1, float s2, float s3) {
float s;
s = (s1 + s2 + s3) / 2.0;
return sqrt (s * (s - s1) * (s - s2) * (s - s3));
}
float distance (int x1, int y1, int x2, int y2) {
float sq;
sq = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
return sqrt (sq);
}
int main() {
int x1, y1, x2, y2, x3, y3;
float s1, s2, s3, area;
int isValid = 0;
printf ("Enter the coordinates of 3 vertices of the triangle: \n" ) ;
printf ("First Vertex (x, y): \n" );
scanf ("%d %d", &x1, &y1);
printf ("Second Vertex (x, y): \n");
scanf ("%d %d", &x2, &y2);
printf ("Third Vertex (x, y): \n");
scanf ("%d %d", &x3, &y3);
s1 = distance (x1, y1, x2, y2);
s2 = distance (x2, y2, x3, y3);
s3 = distance (x1, y1, x3, y3);
printf ("Length of first side = %f\n", s1);
printf ("Length of second side = %f\n", s2);
printf ("Length of third side = %f\n", s3);
isValid = isTriangleValid (s1, s2, s3);
if (isValid) {
area = areaOfTriangle (s1, s2, s3);
printf ("Area: %f\n", area);
}
else
printf ("Not a triangle. \n");
return 0;
}Explanation
Given the three vertices, the program calls the distance() function to obtain the length of three sides joining these vertices.
Then, it calls the isTriangleValid() to check whether the three sides form a valid triangle or not. If they do, it calls the areaOfTriangle() function to calculate the area of the triangle using Heron’s formula, which is given below:
S =
Area =