0
\$\begingroup\$

I'm following this MATHguide lesson on combining vectors and have reached this part:

Next, we need to convert their forces to horizontal and vertical components. Let's use k for Kim's vector and n for Noah's vector.

k = <30, 130°> = <30 cos 130°, 30 sin 130°> = <-19.3, 23.0>. n = <40, 65°> = <40 cos 65°, 40 sin 65°> = <16.9, 36.3>.

To get the X part of Kim's vector, I run 30 * Math.cos(130), I get -11.018739913640896, which is not -19.3. To get the Y part of Kim's vector, I run 30 * Math.sin(130) which is -27.903178505602853, which is not the 23.0 listed by the example.

Where did I go wrong?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

In Javascript, as usual, Math.sin and Math.cos want the angle parameter in radians. You are feeding it degrees, and that's why your results are off.

You probably want to introduce an utility function toRadians, which takes in the angle in degrees and returns the angle in radians.

function toRadians(angle) {
    return (angle * Math.PI) / 180.0; 
}

You can find references all over the internet about angle conversions, but that's how it's done.

Now, if you execute Math.cos(toRadians(130)) * 30, you get about -19.3, which seems to be what you were looking for.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .