Discuss Scratch

Chad_mah
Scratcher
7 posts

How does Scratch's Cos,Sin, and Tan functions work

([Sin v] of (45))
([Cos v] of (45))
([Tan v] of (45))

i do not undersatnd how it gets the sine cosine and tangent from only the angle
if somebody could tell me how the scratch functions work or give me a formula/equation showing how it works to get the sin,cos,and tan. this will be useful for outside scratch and save me alot of time.
This link may explain why im confused

the example below \/ is from the link above /\

sin(35°) =
Opposite
Hypotenuse
=
2.8
4.9
= 0.57…


cos(35°) =
Adjacent
Hypotenuse

=
4.0
4.9
= 0.82…

tan(35°) =
Opposite
Adjacent

=
2.8
4.0

= 0.70…
bobbycatjoe3
Scratcher
20 posts

How does Scratch's Cos,Sin, and Tan functions work

(Also not an expert in math)

according to the Scratch Wikipedia,
“Using this block in Scratch will output the result in degrees. To convert it to radians, multiply the value by 0.01745329251.”

and if you search further, the cos/sine/tan functions run using the CORDIC algorithm (from what I've seen).

don't take this too seriously
Jonathan50
Scratcher
1000+ posts

How does Scratch's Cos,Sin, and Tan functions work

Take sine for example. Notice that the opposite and hypotenuse depend on the size of the right triangle, but the ratio opposite/hypotenuse can stay the same for bigger triangles. Since a bigger triangle with the same opposite/hypotenuse is a similar triangle, the angle is also the same.


(drawing not to scale)

The ratio (opposite/hypotenuse) and the angle are the same no matter how big or small the triangle is. So you don't necessarily need to know the sides of the right triangle. Sin just asks, “If I have the angle, what is opposite/hypotenuse?” The other trigonometric functions are similar. If you don't know the sides, then you can still find the sine from the angle, just by drawing any size right triangle with that angle and taking the ratio.

(There isn't any simple formula to find an exact answer because they're transcendental functions. You can memorize some simple ones though, or use the sin/cos/tan buttons on a scientific calculator. Then Scratch or the calculator uses an algorithm to approximate it.)

Scratch also has inverse trigonometric functions that do the opposite, like asin (also called arcsin and sin^-1). They take a ratio and give back the angle.

Also, right triangles with a hypotenuse of 1 are very convenient. Then sin θ = opposite/1 = opposite. So the sine can be thought of as the length of the opposite given the angle of such a right triangle, and the cosine is the adjacent.

You might also want to find out about the unit circle (and the Pythagorean theorem, if you don't already know about it – it also defines circles).

Last edited by Jonathan50 (Dec. 9, 2022 21:10:34)

Chad_mah
Scratcher
7 posts

How does Scratch's Cos,Sin, and Tan functions work

bobbycatjoe3 wrote:

(Also not an expert in math)

according to the Scratch Wikipedia,
“Using this block in Scratch will output the result in degrees. To convert it to radians, multiply the value by 0.01745329251.”

and if you search further, the cos/sine/tan functions run using the CORDIC algorithm (from what I've seen).

don't take this too seriously


Thanks searched CORDIC algorithm and found this site: redirect.cs.umbc.edu Top of slide 6 is where i realized how to do it. Its very easy once you first figure it out.

here is project with the functions remade
https://scratch.mit.edu/projects/774705321/

Last edited by Chad_mah (Dec. 12, 2022 15:45:28)

mybearworld
Scratcher
1000+ posts

How does Scratch's Cos,Sin, and Tan functions work

bobbycatjoe3 wrote:

according to the Scratch Wikipedia,
Wikipedia is actually only this specific site - the site you mean for is called “Scratch Wiki”.
Chad_mah
Scratcher
7 posts

How does Scratch's Cos,Sin, and Tan functions work

mybearworld wrote:

bobbycatjoe3 wrote:

according to the Scratch Wikipedia,
Wikipedia is actually only this specific site - the site you mean for is called “Scratch Wiki”.

“typo” doesnt rly matter bc you still know what it means
ISTILLMAKESTUFF
Scratcher
500+ posts

How does Scratch's Cos,Sin, and Tan functions work

Scratch is written in JS, so it's only reasonable to use these:
return Math.sin(input);
return Math.cos(input);
return Math.tan(input);

Come on guys, I thought we knew that languages have this feature
davidtheplatform
Scratcher
500+ posts

How does Scratch's Cos,Sin, and Tan functions work

ISTILLMAKESTUFF wrote:

Scratch is written in JS, so it's only reasonable to use these:
return Math.sin(input);
return Math.cos(input);
return Math.tan(input);

Come on guys, I thought we knew that languages have this feature
case 'sin': return parseFloat(Math.sin((Math.PI * n) / 180).toFixed(10));
Here's the code for that block
It does use Math.sin, the extra stuff converts from radians to degrees
Jonathan50
Scratcher
1000+ posts

How does Scratch's Cos,Sin, and Tan functions work

ISTILLMAKESTUFF wrote:

Scratch is written in JS, so it's only reasonable to use these:
return Math.sin(input);
return Math.cos(input);
return Math.tan(input);

Come on guys, I thought we knew that languages have this feature
I didn't think that's what OP wanted to know (though judging by post #4 it could've been), since it seemed to be about how the trigonometric functions take the angle but not the sides.

Worth noting trigonometric functions in other languages (including JS) usually use radians, so for degrees it'd be:
Math.sin(angle * Math.PI/180)
Math.asin(x) * 180/Math.PI
Math.atan2(y, x) * 180/Math.PI
which is effectively what Scratch does.

Edit: Ninja'd

Last edited by Jonathan50 (Dec. 20, 2022 04:21:55)

Chad_mah
Scratcher
7 posts

How does Scratch's Cos,Sin, and Tan functions work

davidtheplatform wrote:

ISTILLMAKESTUFF wrote:

Scratch is written in JS, so it's only reasonable to use these:
return Math.sin(input);
return Math.cos(input);
return Math.tan(input);

Come on guys, I thought we knew that languages have this feature
case 'sin': return parseFloat(Math.sin((Math.PI * n) / 180).toFixed(10));
Here's the code for that block
It does use Math.sin, the extra stuff converts from radians to degrees


the solution to me problem has been found using return Math.sin(input); wont work for what it gonna be used in

Powered by DjangoBB