Use Trigonometric Ratios to Find Unknowns
In this step, we will extend our triangle solver program to calculate unknown sides and angles using trigonometric ratios. We'll update the previous code to include functions for solving right-angled triangles.
Let's modify our triangle_solver.c
file to add calculation functions:
// ~/project/triangle_solver.c
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
struct RightTriangle {
double side_a; // Adjacent side
double side_b; // Opposite side
double side_c; // Hypotenuse
double angle_A; // Angle opposite to side a
double angle_B; // Angle opposite to side b
double angle_C; // Right angle (90 degrees)
};
// Function to convert degrees to radians
double to_radians(double degrees) {
return degrees * (PI / 180.0);
}
// Function to convert radians to degrees
double to_degrees(double radians) {
return radians * (180.0 / PI);
}
// Calculate missing sides using trigonometric ratios
void solve_triangle(struct RightTriangle *triangle) {
// If hypotenuse and one side are known, calculate the third side
if (triangle->side_c > 0 && triangle->side_a > 0 && triangle->side_b == 0) {
triangle->side_b = sqrt(triangle->side_c * triangle->side_c - triangle->side_a * triangle->side_a);
}
// If two sides are known, calculate angles
if (triangle->side_a > 0 && triangle->side_c > 0) {
triangle->angle_A = to_degrees(asin(triangle->side_a / triangle->side_c));
triangle->angle_B = 90.0 - triangle->angle_A;
}
// If two sides are known, calculate hypotenuse
if (triangle->side_a > 0 && triangle->side_b > 0 && triangle->side_c == 0) {
triangle->side_c = sqrt(triangle->side_a * triangle->side_a + triangle->side_b * triangle->side_b);
}
}
// Print triangle information
void print_triangle_info(struct RightTriangle *triangle) {
printf("\nTriangle Information:\n");
printf("Side a: %.2f\n", triangle->side_a);
printf("Side b: %.2f\n", triangle->side_b);
printf("Side c (Hypotenuse): %.2f\n", triangle->side_c);
printf("Angle A: %.2f degrees\n", triangle->angle_A);
printf("Angle B: %.2f degrees\n", triangle->angle_B);
printf("Angle C: 90.00 degrees\n");
}
int main() {
struct RightTriangle triangle = {0}; // Initialize all values to 0
// Input known sides or angles
printf("Enter known side/angle values (enter 0 if unknown):\n");
printf("Side a length: ");
scanf("%lf", &triangle.side_a);
printf("Side b length: ");
scanf("%lf", &triangle.side_b);
printf("Side c (hypotenuse) length: ");
scanf("%lf", &triangle.side_c);
printf("Angle A (in degrees): ");
scanf("%lf", &triangle.angle_A);
printf("Angle B (in degrees): ");
scanf("%lf", &triangle.angle_B);
// Solve for unknown values
solve_triangle(&triangle);
// Print results
print_triangle_info(&triangle);
return 0;
}
Now, let's compile and run the updated program:
gcc ~/project/triangle_solver.c -o ~/project/triangle_solver -lm
~/project/triangle_solver
Example input and output:
Enter known side/angle values (enter 0 if unknown):
Side a length: 3
Side b length: 4
Side c (hypotenuse) length: 0
Angle A (in degrees): 0
Angle B (in degrees): 0
Triangle Information:
Side a: 3.00
Side b: 4.00
Side c (Hypotenuse): 5.00
Angle A: 36.87 degrees
Angle B: 53.13 degrees
Angle C: 90.00 degrees
Understanding Trigonometric Calculations
solve_triangle()
uses trigonometric ratios to calculate missing values:
- Pythagorean theorem for side calculations
asin()
for angle calculations
to_radians()
and to_degrees()
help convert between degrees and radians
- The program can solve the triangle with various input combinations
Key Trigonometric Relationships
- Sine: opposite / hypotenuse
- Cosine: adjacent / hypotenuse
- Tangent: opposite / adjacent
- Pythagorean theorem: a² + b² = c²