The following text introduces you to beginning algebra, geometry, and trigonometry. It is not necessary that you have taken these courses to allow you to follow the methods used.
In the last issue, we had a brief introduction to the use of RADIAN angles, which we'll continue in this article. When you become accustomed to using radian measurement, you will have no more difficulty than converting feet to yards, or ounces to pounds.
In this article we'll use a right-triangle, which everyone should be familiar with. When solving triangle problems, we usually need three values to work with, but we know a right-triangle has one angle of 90°, which means we can solve all problems using only two additional values.

We'll get user input from any two of five textboxes and use error trapping to be sure we have proper input without causing a program crash. Next we'll use multiple SELECT CASE statements to determine how to calculate values not given in the problem. This program may come in handy for several projects around the home, or for checking your math homework. You'll need to work your homework problems first, because your teacher will want to see the steps you used to solve the problem. The right-triangle program can be used to check your work before you turn it in. You'll need to download the zip archive to get the entire code and help file.
First we'll set up a window with a graphicbox showing a typical right-triangle. Text boxes will allow the user to input known values for Angle A or B, or values for any of the three sides. We can solve the right-triangle if we have at least two sides, because we know "the square of the hypotenuse is equal to the sum of the squares of the remaining sides."
[solve] 'Get user input
PRINT #rt.tbx1, "!contents? B"
PRINT #rt.tbx2, "!contents? c"
PRINT #rt.tbx3, "!contents? a"
PRINT #rt.tbx4, "!contents? A"
PRINT #rt.tbx5, "!contents? b"
'====Catch input errors here
IF a=0 AND b=0 AND c=0 THEN [msg1]
IF a<0 OR b<0 OR c<0 OR A<0 or B<0 THEN [msg2]
IF A=0 AND B=0 AND C=0 AND a=0 AND b=0 THEN [msg3]
IF A=0 AND B=0 AND C=0 AND a=0 AND c=0 THEN [msg3]
IF A=0 AND B=0 AND C=0 AND b=0 AND c=0 THEN [msg3]
IF A+B>90 THEN [msg5]
IF A>90 OR B>90 THEN [msg5]
When the user has submitted input, we first check for correct values to reduce program crashes. If no values for a side have been entered, msg1 is displayed. If negative values have been entered, the user is playing with us and msg2 is displayed. Msg3 checks to see if at least two values have been entered, we can't fly on only one wing. We've tried to anticipate common errors, but if there's a way to crash the program, someone will find it! One final error check will follow the SELECT CASE statement using CASE ELSE. If none of the missing values suit my program, or if the user has entered too many values, msg4 is displayed.
If the input is acceptable, we move on to a SELECT CASE routine and choose which method will be used for calculations, based upon the known information provided. You'll see there are alternate methods for determining unknown values.
'====If no errors, choose method to solve
SELECT CASE
CASE (A=0) AND (B=0) AND (c=0) 'When a and b are given
c=SQR(a^2+b^2) 'Pythagorean Theorem solves for c
A=a/c 'Calculate the tangent of A
A=asn(A) 'Convert tan(A) to radian angle
B=(sin(A)/a)*b 'First method, using sin(A) to solve for B.
B=asn(B) 'Convert sin(B) to radian angle
GOSUB [showResults]
CASE (A=0) AND (B=0) AND (a=0) 'When b and c are given
a=SQR(c^2-b^2)
A=a/c 'Calculate tan(A)
A=asn(A) 'Convert tan(A) to radians
B=(pi/2)-A 'Another method, using pi/2 to solve for B.
GOSUB [showResults]
CASE (A=0) AND (B=0) AND (b=0) 'When a and c are given
b=SQR(c^2-a^2)
A=a/c
A=asn(A)
B=(pi/2)-A
GOSUB [showResults]
CASE (B=0) AND (b=0) AND (c=0) 'When A and a are given
A=A/rads
B=(pi/2)-A
b=sin(B)/(sin(A)/a)
c=a/sin(A)
GOSUB [showResults]
CASE (B=0) AND (a=0) AND (c=0) 'When A and b are given
A=A/rads
B=(pi/2)-A
a=sin(A)/(sin(B)/b)
c=SQR(a^2+b^2)
GOSUB [showResults]
CASE (B=0) AND (a=0) AND (b=0) 'When A and c are given
A=A/rads
B=(pi/2)-A
a=sin(A)*c
b=SQR(c^2-a^2)
GOSUB [showResults]
CASE (A=0) AND (b=0) AND (c=0) 'When B and a are given
B=B/rads
A=(pi/2)-B
b=sin(B)/(sin(A)/a)
c=SQR(a^2+b^2)
GOSUB [showResults]
CASE (A=0) AND (a=0) AND (c=0) 'When B and b are given
B=B/rads
A=(pi/2)-B
a=sin(A)/(sin(B)/b)
c=SQR(a^2+b^2)
GOSUB [showResults]
CASE (A=0) AND (a=0) AND (b=0) 'When B and c are given
B=B/rads
A=(pi/2)-B
a=c*sin(A)
b=SQR(c^2-a^2)
GOSUB [showResults]
CASE ELSE
GOTO [msg4]
END SELECT
SELECT CASE is a wonderful tool, frequently reducing a large number of IF/THEN statements to just a few lines of code. One way to find out what the user has entered is to determine if a value is greater than zero, IF A>0 AND B>0...THEN... etc. We can't test for an exact value because we don't know what may be entered, so we check to see if anything has been entered. In this routine, I decided to check for non-entries, that is, IF A=0 AND B=0...THEN... etc. By determining which values have not been given, I will know which values are available to solve the problem. Only 5 values can be entered, and each CASE statement checks to see which combination of values are zero.
One of the easiest solutions for a right triangle, is the calculation of any missing side, which are my first three comparisons. From the Pythagorean Theorem, we know c²=a²+b². By substitution, we can calculate the length of the missing side, but we haven't solved the problem yet, we must have the value of Angle A or Angle B to solve for all unknowns.
>Remember, JB uses radians for angular measurement. The circumference of any circle is C=2pi*r. I set pi=3.141592654 at the beginning of the program, because I will use this value several times and variable pi is much easier to type than the literal value. We'll also need to convert degrees to radians, and vice-versa, in several places, so I assigned rads=57.29577951 early in the program.
In grade school, we learned there are 360° in a full circle, or 6.283185307 radians. Half a circle is 180°, or pi radians. We also learned the sum of the angles of any triangle equals 180°. Lastly, we know one angle of a right triangle equals 90° (1.570796327 radians), so the sum of the remaining two angles is A+B=1.570796327 rads.
In any right triangle, we know tan(A)=a/c; all we need to do now is convert the tangent value to an angle. We use the arcsine function, A=asn(A) to get the angular measurement of A in radians, then subtract A from 1.570796327 (Angle C, the right angle), to get Angle B.
Another method to determine Angle B is the ratios known using the Law of Sines: sin(A)/a = sin(B)/b = sin(C)/c. Once we have calculated Angle A using the tangent function, we can substitute sin(B)=(sin(A)/a)*b. Either method produces the same result, with possibly a tiny difference after the sixth decimal place. After calculating sin(B), it is only necessary to convert it to radians using B=sin(B)*rads.
So far, we have solved for all values when the length of any two sides are known. The next case is to solve when one angle and one side is given. In this case, we first calculate the angle, A/rads=(pi/2)-B, or vice-versa. Angle C is always equal to (pi/2) because it is a right angle. When we subtract A or B, the result is the unknown angle.
If Angle A = 30° (.523598775 rads), Angle B = 1.047197551 rads. Now that we know all three angles, we can solve for the unknown sides. When a is known, and substituting the ratios from the Law of Sines, b=(sin(b)/(sin(A)/a). When b, not a, is known, a=sin(A)/(sin(B)/b)
When all values have been calculated, we GOSUB to a print routine for display. At this time, all angles are in radians, so we multiply by rads to display the value in degrees. Area measurement is included in the event you have to shop for carpeting or roofing paper. If your measurements are in inches, the area is sq/inches. If your measurements are in feet, centimeters, or meters, the area will be sq/ft, sq/cm, or sq/meters.
This program is for right-triangles only. Other triangles can be solved using more trigonometry, but will require at least three of six values to be entered in the program. Naturally, that will require more error checking and SELECT CASE comparisons, but you've had a brief introduction to accomplish these tasks.
You will not need an advanced degree in mathematics to write computer programs, but even game animations will require some understanding of algebra, geometry, and trigonometry. Take all the math courses you can while in school, and don't be afraid to ask yourself, "What happens if I...?"
Unfortunately, you will not always have a helpful Worble around to solve problems for you.