Main content

## Computer programming

### Course: Computer programming > Unit 5

Lesson 6: Angular Movement- Angles and units
- Challenge: Spinning baton
- Angular velocity
- Challenge: Falling boulder
- Trigonometry
- Trigonometric ratios in right triangles
- Pointing towards movement
- Challenge: Turning car
- Polar coordinates
- Challenge: Spiral drawer
- Project: Asteroids spaceship

© 2023 Khan AcademyTerms of usePrivacy PolicyCookie Notice

# Trigonometry

I think it may be time. We’ve looked at angles, we’ve spun an object. It’s time for:

*soh cah toa*. Yes,*soh cah toa*. This seemingly nonsensical word is actually the foundation for a lot of computer graphics work. A basic understanding of trigonometry is essential if you want to calculate an angle, figure out the distance between points, work with circles, arcs, or lines. And*soh cah toa*is a mnemonic device (albeit a somewhat absurd one) for what the trigonometric functions sine, cosine, and tangent mean.: sine = opposite / hypotenuse*soh*: cosine = adjacent / hypotenuse*cah*: tangent = opposite / adjacent*toa*

Take a look at the above diagram again. There’s no need to memorize it, but make sure you feel comfortable with it. Draw it again yourself. Now let’s draw it a slightly different way:

See how we create a right triangle out of a vector? The vector arrow itself is the hypotenuse and the components of the vector (

`x`

and `y`

) are the sides of the triangle. The angle is an additional means for specifying the vector’s direction (or “heading”).Because the trigonometric functions allow us to establish a relationship between the components of a vector and its direction + magnitude, they will prove very useful throughout this course. We’ll begin by looking at an example that requires the tangent function.

This "Natural Simulations" course is a derivative of "The Nature of Code" by Daniel Shiffman, used under a Creative Commons Attribution-NonCommercial 3.0 Unported License.

## Want to join the conversation?

- what do i do when I have to find a side, i know how to do angle and all that, but its really difficult to know when to do 4/cos(23 or 6xsin(9(4 votes)
- I don't get still what's a soh, cah, and toa.(2 votes)
- Given a non-90° angle in a right triangle:

soh: the Sine is the ratio of Opposite over Hypotenuse.

cah: the Cosine is the ratio of Adjacent over Hypotenuse.

toa: the Tangent is the ratio of Opposite over Adjacent.(7 votes)

- I answer one question on the quiz one way, then another triangle comes up. Same shape, so I assume the adjacent, opposite, and hypotenuse are located on the same sides, but when I go to do it, it says I'm wrong. How do you determine which is the hypotenuse, adjacent and opposite?(2 votes)
- No, even if the shapes are the same, the angle is different therefore the opposite, hypotenuse, and adjancents are different as well.

Opposite: opposite of the angle

Hypotenuse: the longest side (so if its the same shape this should be the same)

Adjacent: kind of next to the angle, the nearest one

Hope I helped!(2 votes)

- i need a tip what if they gave me two angles but no sides then what do i use(2 votes)
- It depends on what you are looking for. Since the angles of a triangle to sum to 180° or π radians you can easily find the measure of the mystery third angle.(2 votes)

- I learned sin cos and tan, but I don't understand how to use it in programming. I saw your Hypnoswirl program but I did not understand how sin and cos helped to make the program.(1 vote)
- If you have a PVector, you can use the tangent [atan2(x,y)] function to find the angle it is pointing in. If you have an angle, you can use cos() and sin() functions to find values for x and y. If you need to model anything periodic sin and cos are useful.

Be aware, though, processingjs uses a weird convention of +Y being down instead of up. That makes everything rotate backwards.(3 votes)

- so are you basiclly trying to find the missing side?(1 vote)
- If you know two sides of a right triangle, you can find the missing side with the Pythagorean Theorem.

But if you know a side and an angle (other than the right angle), then you can use tan, cos or sin to find the other sides.

So yeah, you're right!

You can also work in the other direction: you can find the angles if you know two sides.(2 votes)

- I know how to use the
`tan()`

function, but what if I need to take the anti-tan or opposite tan of something. Is there a function for that? O.o(1 vote)`atan`

is the anti-tangent function. Be aware that most people find`atan2`

is more desirable than`atan`

. While`atan`

only returns angles between -π/2 (-90°) and +π/2 (90°),`atan2`

returns all possible values, -π (-180°) to +π/2 (180°).(1 vote)

- Heres my code... how would i find the angle of the red dot, to the center, to the white dot?

var x1 = random(99, 301);

var y1 = random(99, 301);

var x2 = random(99, 301);

var y2 = random(99, 301);

var t = dist(mouseX, mouseY, 200, 200);

var di1 = dist(x1, y1, 200, 200);

var di2 = dist(x2, y2, 200, 200);

draw = function() {

background(255, 255, 255);

fill(0, 0, 0);

di1 = round(dist(x1, y1, 200, 200));

ellipse(200, 200, 280, 280);

fill(255, 0, 0);

ellipse(x1, y1, 20, 20);

fill(255, 255, 255);

ellipse(x2, y2, 20, 20);

fill(0, 0, 0);

text(di1, 30, 30);

if(di1 !== 140){

x1 = random(99, 301);

y1 = random(99, 301);

}

};(1 vote)- The angle
*from*the red dot at`(x1, y1)`

*to*the white dot at`(x2, y2)`

is computed with`var theta = atan2(y2 - y1, x2 - x1);`

(1 vote)

- i don't understand this at all.(1 vote)
- It is recommended that you take this course after or during high school, because a lot of the math in here is advanced. Khan Academy does have some very helpful videos if you want to learn trigonometry.(1 vote)