This Pascal Code is what motivated me to create the Area Program above. You will see that the same menu is displayed, but that more must be considered in each case, since in some situations we will need to use the Law of Cosines or the Law of Sines to find lengths of sides, and in other situations we need to use the Law of Cosines or the Law of Sines to compute angle measures.
While
it is important to apply these methods by hand with pencil and paper, drawing a figure, labeling the given data and unknowns, and showing yourself how to find altitude and base, basically reconstructing the formulas in a natural, non-memorized manner, writing code and running various scenarios forced me to develop a systematic strategy. It forced me to really study the various cases, especially the ambiguous case where two different triangles are possible from the data. There was certain data which do not determine a triangle because the sum of two side lengths were greater than the length of the third side. Other data would provide inverse trig functions with values which were not defined for the domain, for instance, feeding arc cosine values less than -1 or greater than 1, so, in these cases the data does not determine a triangle either. These values had to be "caught" so that they were not stupidly fed to the inverse function, Then I used a goto and label so as to exit the program gracefully, rather than allow it to produce garbled erroneous output or just crash and burn.
In the C program I did not have to use any goto/label strategy, but, rather, I just made the functions return integer (rather than void). This way, when I wanted to exit gracefully, I just had to "return 0;".
Hence, another example of just how greatly computer programming can complement mathematical understanding in the process of solving the problem methodically.
So, as I noticed there is not much code available on the Internet which handles all the cases, I am including this as well as the above C code. Maybe someone will be pleased to stumble upon it in our little shop of philosophical horrors. i am sure that if you do a comparison, you will find the code in these last two posts is of a different caliber than those clogging up the ZooTubes with programs that handle only one scenario. It's kind of pitiful, actually. It makes me wonder about how the search engines out there have gone down the tubes over the years, most likely a consequence of advertisements and money which bump certain sites up to the top of the hit list. It's a shame that, for instance, you can't just type
C Program for finding Area of a Triangle to find the code I just wrote
here. What's going on with the search engines? Even explicitly searching for
whybother.freeboards.org "C Program for finding Area of a Triangle" does not find us here. This leads me to expect that our message board is for all intents and purposes off the grid. Maybe it's not such a bad thing, but it makes me feel so utterly invisible ... Fuuck it. Two tears in a bucket.
Oh well. Maybe someone besides myself will appreciate the thoroughness.
This next one can be compiled with
Lazarus.
(*********************************************************************)
program solve_triangle (INPUT, OUTPUT);
type
flag = 1..2;
var
a, b, c, b2, c2 : real;
angle_a, angle_b, angle_c, angle_b2, angle_c2 : real;
pi : real;
option : integer;
second_solution : boolean;
no_solution : boolean;
(**************************************************************************)
procedure display_menu (var option : integer);
begin
writeln('What is your option?');
writeln('<1> 2 sides and included angle');
writeln('<2> 2 angles and included side');
writeln('<3> 3 sides');
writeln('<4> 2 sides and angle opposite one of them');
write('ENTER: ');
readln(option);
writeln;
end;
(**************************************************************************)
procedure get_data (option : integer);
begin
case option of
1: begin
write('Enter the length of a side: ');
readln(a);
write('Enter the length of the other side: ');
readln(b);
write('Enter the measure of the included angle: ');
readln(angle_c);
end;
2: begin
write('Enter the measure of an angle: ');
readln(angle_a);
write('Enter the measure of the other angle: ');
readln(angle_b);
angle_c := 180 - (angle_a + angle_b);
write('Enter the length of the included side: ');
readln(c);
end;
3: begin
write('Enter the length of a side: ');
readln(a);
write('Enter the length of another side: ');
readln(b);
write('Enter the length of the third side: ');
readln(c);
end;
4: begin
write('Enter the measure of the angle: ');
readln(angle_a);
write('Enter the length of the side opposite this angle: ');
readln(a);
write('Enter the length of the other side: ');
readln(b);
end;
end; (* case *)
writeln;
end;
(* The flag tells the procedure whether to solve for an angle *)
(* or a side using the Law of Cosines *)
(***************************************************************)
procedure law_of_cosines (var c, ang_c : real; a, b : real; choice : flag);
label
Exit;
var
cosine : real;
begin
if choice = 1
then
begin
c := sqrt(a * a + b * b - 2 * a * b * cos(pi * ang_c/180));
if (a + b < c) then begin
writeln('NO SOLUTION');
no_solution := TRUE;
writeln('Since ', a:1:3, ' + ', b:1:3, ' = ', (a+b):1:3, ' < ', c:1:3, ',');
writeln('there is no triangle with the given sides.');
end;
end
else
begin
cosine := (c * c - a * a - b * b)/(-2 * a * b);
if (a + b < c) then begin
writeln('NO SOLUTION');
no_solution := TRUE;
writeln('Since ', a:1:3, ' + ', b:1:3, ' = ', (a+b):1:3, ' < ', c:1:3, ',');
writeln('there is no triangle with the given sides.');
end;
if (no_solution) then begin
writeln('We will procede to exit immediately if not sooner.');
GoTo Exit;
end;
if cosine > 0
then begin
ang_c := arctan(sqrt(1 - cosine * cosine)/cosine);
ang_c := 180 * ang_c/pi;
end;
if cosine < 0
then begin
cosine := -cosine;
ang_c := arctan(sqrt(1 - cosine * cosine)/cosine);
ang_c := 180 - 180 * ang_c/pi;
end;
if cosine = 0
then ang_c := 90;
end;
Exit:
end;
(* The flag tells the procedure whether to solve for an angle *)
(* or a side using the Law of Sines *)
(***************************************************************)
procedure law_of_sines (var a, ang_a : real; b, ang_b : real;
choice : flag);
var
sine : real;
begin
if choice = 2
then begin
sine := a * sin(pi * ang_b/180)/b;
if sine > 0
then begin
ang_a := arctan(sine/sqrt(1 - sine * sine));
ang_a := 180 * ang_a/pi;
end;
if sine < 0
then begin
sine := -sine;
ang_a := arctan(sine/sqrt(1 - sine * sine));
ang_a := 180 - 180 * ang_a/pi;
end;
end
else a := b * sin(pi * ang_a/180)/(sin(pi * ang_b/180));
end;
(*************************************************************************)
procedure display_results;
begin
if (no_solution)
then writeln('No results to display.')
else
begin
write('The lengths of the sides are: ');
writeln(a:1:3, ' ', b:1:3, ' ', c:1:3);
write('The measures of the angles are: ');
writeln(angle_a:1:3, ' ', angle_b:1:3, ' ', angle_c:1:3);
writeln;
if second_solution
then begin
writeln('OR');
writeln;
write('The lengths of the sides are: ');
writeln(a:1:3, ' ', b2:1:3, ' ', c2:1:3);
write('The measures of the angles are: ');
write(angle_a:1:3, ' ', angle_b2:1:3, ' ');
writeln(angle_c2:1:3);
end;
end;
end;
(************************************************************************)
begin (* main *)
display_menu(option);
get_data(option);
pi := 4 * arctan(1);
second_solution := FALSE;
no_solution := FALSE;
case option of
1: begin
law_of_cosines(c, angle_c, a, b, 1);
law_of_sines(a, angle_a, c, angle_c, 2);
angle_b := 180 - (angle_a + angle_c);
end;
2: begin
law_of_sines(a, angle_a, c, angle_c, 1);
law_of_sines(b, angle_b, c, angle_c, 1);
end;
3: begin
law_of_cosines(c, angle_c, a, b, 2);
law_of_sines(a, angle_a, c, angle_c, 2);
angle_b := 180 - (angle_a + angle_c);
end;
4: begin
if angle_a < 90
then begin
if a < b * sin(pi * angle_a/180)
then begin
writeln('NO SOLUTION');
no_solution := TRUE;
writeln('angle < 90 AND length ', a:1:3, ' < ', b:1:3, '*sin(', (angle_a):1:3, ')');
end
else begin
law_of_sines(b, angle_b, a, angle_a, 2);
angle_c := 180 - (angle_a + angle_b);
law_of_sines(c, angle_c, a, angle_a, 1);
end;
if (b * sin(pi * angle_a/180) < a) and (a < b)
then begin
angle_b2 := 180 - angle_b;
angle_c2 := 180 - (angle_a + angle_b2);
law_of_sines(b2, angle_b2, a, angle_a, 1);
law_of_sines(c2, angle_c2, a, angle_a, 1);
second_solution := TRUE;
end;
end;
if (90 <= angle_a) and (angle_a < 180)
then if a <= b
then begin
writeln('NO SOLUTION');
writeln('90 <= angle < 180 AND ', a:1:3, ' <= ', b:1:3);
no_solution := TRUE;
end
else begin
law_of_sines(b, angle_b, a, angle_a, 2);
angle_c := 180 - (angle_a + angle_b);
law_of_sines(c, angle_c, a, angle_a, 1);
end;
end;
end; (* case *)
display_results;
end.