Coding for Kids

Using repl.it to Code

Trigonometry

Coding for Kids Sidenote: This dips into quite a bit of mathematics. This gives a basic tutorial on trigonometry. You may not have learned that yet, so feel free to skip this section.

If you haven't learned trig yet, this may not make much sense. In short, for a given right triangle, if you have an angle and the length of a side, you can find the length of another side. Or if you have two sides, you can find an angle. First a little math lesson before we get back to the Math object.

Theta (θ) is the angle we're looking at. Side x is called the adjacent to the angle. Side y is opposite the angle. Side h is the hypotenuse.

Coding for Kids
Basic right triangle for trigonometry

We can use sine (sin), cosine (cos), and tangent (tan) to find any piece we're missing if we have two other values. All three of these trig functions use the angle and two sides.

Coding for Kids
Basic trigonometry formulas

If you have an angle of 30° and an adjacent of 15, you can figure out the opposite using tangent (or the hypotenuse using cosine). We use this to figure out high rockets fly. We know how far away from the base we are and we can measure the angle of ascent. With some trigonometry using tangent, we can calculate how high the rockets go.

That's all nice and good, but what on Earth is the tangent of 30°?

This is where the Math object helps us out. There is one slight complication though. The Math object does not use degrees! It measures angles in radians. A radian is a circle that has an arc equal to its radius. An entire circle encompasses 360°, which is 2π radians. Any time we want to use trig in degrees, you have to convert the angle one way or the other.

The formulas are:

So, to get that tan of 30°, you need this.

Let's see how high that rocket went. If we were 15 meters (almost 50 feet) away from the launch pad and we measured an angle of 30°...

The opposite represents how high up it went, so 8.66 meters, which is about 28 feet!

The basic trig functions are:

But what if you have two sides and you need to get the angle? Using any of the formulas, you'd get some number equal to sin θ or cos θ or tan θ. How do you get θ?

You would have to use the inverse of the trig functions, which I call the "arcs": arcsine, arccosine, and arctangent.

Let's work that last rocket question backwards.

I used the same situation and just worked backwards and look… we got the same angle! It's different because of some rounding, but 29.999° is really 30°, isn't it?

There's even more to the Math object than that but they're rather specific and intended for upper level math stuff. Search it out if you're interested!
JavaScript Math Object

It's a sine of the times when I go off on a tangent!

—Dr. Wolf