Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Codecombat geometry: What does Math.cos(angle) and Math.sin(angle) mean?

    defendPos  = { "x" : 41 , "y" : 40 }
    defendPos . x  +=  10  *  Math.cos (angle)
    defendPos . y  +=  10  *  Math.sin (angle)

What does the three lines of code represent. I want like a translation in some sort of way :smiley:

Il me semble que c’est pour donner un certain type d’angle. J’ai plus ou mois étudier la chose sur un autre site de programmation de jeu. Et au lieu de donner un chiffre en dur, on lui attribut le math.cos en x. et le math.sin en y. C’est tous simplement de la trigonométrie, on apprend cela en géometrie. Cela te permet de calculer un angle.
J’espere que cela va t’aider.
:pray: :v: :slight_smile:

1 Like

Hi Yeet,

The sin (short for sine) and cos (short for cosine) of an angle describes the ratio between the lengths of the different sides of a triangle. So if you know one of the angles in a (right angle) triangle, and one of the side lengths, you can use the ratios to work out the lengths of the other two sides. This is a really useful thing to be able to do in real life - from working out the height of a tree, to calculating the path a rocket will take on launch! It’s called trigonometry.

For more information about trig, look at a page like this (or Google has plenty of others).

The three lines of code you give will work out the x and y of the position that you’ll get to if you move 10 units in the direction ‘angle’ from your start position. The function with cos gives how far along the x axis you need to go, and the function with sin gives how far along the y axis. Pretty cool :sunglasses: (I think at least).



These should get you hooked up for the Trig levels:
Law of sines: a/sin(a)=b/sin(b)=c/sin(c).
Law of cosines: c^2=a^2+b^2-2ab*cos(c)
There’s more, but from what I remember these are the ones you’ll use most.

1 Like

If I’m not mistaken this part of code is from the level with defending Hushbaum?
These three lines help you to form a circle around (41,40) .
You can go to
and try to use function that draw a circle to change numbers and look what happens)


wait how does the sin and cosine help find the distance of a circle or square?

wait how does the sin and cosine help find the distance of a circle or square???

Hi @Yeet, please could you not repeat your post twice, it won’t get you help any faster.


Look up “Unit Circle” on the web and it’ll tell you how to utilize Trigonometric functions for circular formations.