This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2008-03-21 17:22:49

Lucario621
Community Moderator
Registered: 2007-10-03
Posts: 1000+

Formula for Sine and Cosine

I've been wondering . . . is there any formula to find out sine or cosine of a direction without using the math block? Because inside that block there has to be a formula . . . but what is it? I mean I have and explanation of sine right here:

                                                                ___+++++++++___
                                                     -+++++++                  +  +++++++-
                                           -+++++                             + |               +++++-
                                       -++++                                +    |                   ++++-
                                       -+++                                o      |                     +++-
                                       -++++                                      |                   ++++-
                                           -+++++                                |               +++++-
                                                    -+++++++                   |  +++++++-
                                                                ___++++++++|___
                                                                                      |
                                       -1              -0.5                0     |       0.5               1
                                       |--------------------------------------------------------|

Well it took me 15 min to make, but it was worth it I guess even though I made a horrible circle. As you can see, the direction is about 15 degrees. So the line goes in that direction starting in the middle 'til it touches the circle/oval(sadly) and then the line goes strait down. Now on the number-line it is at about 0.2 so you could say sine of 15=0.2. For cosine the number line would go on the right. and cosine of 15 would be maybe 0.85 I'm guessing. But please, tell me if there is a formula for finding sine or cosine of a direction.


http://i.imgur.com/WBkM2QQ.png

Offline

 

#2 2008-03-21 19:15:19

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: Formula for Sine and Cosine

Sorry I can't help you with this. I am not that good with math :S

Last edited by archmage (2008-03-21 19:15:32)


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#3 2008-03-21 19:35:15

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Formula for Sine and Cosine

Here's a quote directly from Wikipedia:

Wikipedia wrote:

Modern computers use a variety of techniques.[3] One common method, especially on higher-end processors with floating point units, is to combine a polynomial or rational approximation (such as Chebyshev approximation, best uniform approximation, and Padé approximation, and typically for higher or variable precisions, Taylor and Laurent series) with range reduction and a table lookup — they first look up the closest angle in a small table, and then use the polynomial to compute the correction.[4] On simpler devices that lack hardware multipliers, there is an algorithm called CORDIC (as well as related techniques) that is more efficient, since it uses only shifts and additions. All of these methods are commonly implemented in hardware for performance reasons.

For very high precision calculations, when series expansion convergence becomes too slow, trigonometric functions can be approximated by the arithmetic-geometric mean, which itself approximates the trigonometric function by the (complex) elliptic integral.[5]

I got this article from this link

http://en.wikipedia.org/wiki/Cosine#Computation


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#4 2008-03-22 16:48:35

Lucario621
Community Moderator
Registered: 2007-10-03
Posts: 1000+

Re: Formula for Sine and Cosine

Sad to hear archmage. But Paddle2See, honestly that does not help because it makes no sence to me. Like the words polynomial, rational, Chebyshev, Pade, Laurent, CORDIC, eliptic integral. Plus that sadly does not answer my question.


http://i.imgur.com/WBkM2QQ.png

Offline

 

#5 2008-03-22 18:50:59

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Formula for Sine and Cosine

Sorry you did not find it helpful.  The point of the article is that there are lots of different formuli that are used, depending on the accuracy required, the speed required and so on.  Every one of those names represents a different technique.  I belive they were all hyperlinked to more follow-on aritcles that might actually give you an equation.

You know, it's funny, my first calculator didn't have Sine and Cosine on it (those cost real money back then).  So I derived a parabolic equation (with another parabolic correction factor) so I could get approximate trig values.  It worked pretty well, for my purposes (maybe 3 places of accuracy), and impressed my math teacher a lot.  I could probably come up with that equation, if you want it.

I'll see if I can find one of the series formuli too, I know I've tracked them down at some point.


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#6 2008-03-23 03:09:21

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Formula for Sine and Cosine

There are sets of formula for sin, cos and tan that allow you to use the sin (or cos or tan) of the angle of a triangle and the length of a side to calculate the length of another side of the triangle.

If you already know the lengths of the sides and the angle, you could derive Sin.

In scratch, you could have two sprites set apart.  You could se the difference in x to give the length of one side, the distance in y to give the other, and the "distance to" block to get the longest side (the hypotenuse), and the "direction" block to get the angle.

Armed with these values, you are able to derive the Sin, Cos and Tan for any angle.

http://scratch.mit.edu/projects/Mayhem/125960

This project, unfortunately, rounds the variable display to the nearest decimal place so does not show a complely accurate value of Sin Cos or Tan.

It will also return an error should the Adjeacent = zero, ie the two points are directly above each other to form a vertical line.

Last edited by Mayhem (2008-03-23 04:32:57)


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#7 2008-03-23 06:27:28

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Formula for Sine and Cosine

While Mayhem's project admirably shows the releationship between the different trig functions, it is not the way the computer calculates them. 

I have written a project that calculates the Cosine function using the first 5 terms of the Taylor series for that function.  This is one of the types of equation that are used to actually calculate trig functions for a given angle.  Enjoy!

http://scratch.mit.edu/projects/Paddle2SeeFixIt/125979


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#8 2008-07-25 15:07:47

lsquaredmom
Scratcher
Registered: 2008-07-24
Posts: 25

Re: Formula for Sine and Cosine

I was making what I thought of as a simple triangle program based on the "trig fun" triangle program that someone else made.  The 'trig fun" program only dealt with right triangles, whereas mine is supposed to deal with all triangles.  I can't get the program to calculate anything accurate enough to get the angles to add up any where near 180 deg.  The values are just way too far off, but if I tear all of the blocks apart and click on them one at a time, they are basically doing the right thing.

Any suggestions?
The program is called: trig Fun Edited to include all triangles
and you can find it on the lsquaredmom projects page.


http://www.lsquaredmath.us/images/alienSml.jpg

Offline

 

#9 2008-07-25 18:56:22

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Formula for Sine and Cosine

lsquaredmom wrote:

I was making what I thought of as a simple triangle program based on the "trig fun" triangle program that someone else made.  The 'trig fun" program only dealt with right triangles, whereas mine is supposed to deal with all triangles.  I can't get the program to calculate anything accurate enough to get the angles to add up any where near 180 deg.  The values are just way too far off, but if I tear all of the blocks apart and click on them one at a time, they are basically doing the right thing.

Any suggestions?
The program is called: trig Fun Edited to include all triangles
and you can find it on the lsquaredmom projects page.

The basic problem here was that you were using "Broadcast" instead of "Broadcast and Wait"  Broadcast triggers scripts but does not stop execution of the remaining blocks.  By switching to "Broadcast and Wait", the speed and accuracy problems were solved.  I also took the liberty of using the new draggable sprite option and eliminating a lot of code dealing with the dragging of sprites.  My version is here:

http://scratch.mit.edu/projects/Paddle2SeeFixIt/225020


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#10 2008-07-26 07:59:58

lsquaredmom
Scratcher
Registered: 2008-07-24
Posts: 25

Re: Formula for Sine and Cosine

Paddle2See wrote:

The basic problem here was that you were using "Broadcast" instead of "Broadcast and Wait"  Broadcast triggers scripts but does not stop execution of the remaining blocks.  By switching to "Broadcast and Wait", the speed and accuracy problems were solved.  I also took the liberty of using the new draggable sprite option and eliminating a lot of code dealing with the dragging of sprites.  My version is here:

http://scratch.mit.edu/projects/Paddle2SeeFixIt/225020

Thank you, Broadcast and Wait certainly makes sense.  Also I did not know about the draggable sprites.  That will be very useful since I often make programs that involve dragging.  I noticed that your version drags much smoother than what I had posted.

Thanks again!


http://www.lsquaredmath.us/images/alienSml.jpg

Offline

 

#11 2008-07-26 10:46:29

lsquaredmom
Scratcher
Registered: 2008-07-24
Posts: 25

Re: Formula for Sine and Cosine

I put some of the drag detection variables back in because when you are not dragging, I see no reason for the pen to keep drawing.  It makes a slight vibration that is annoying.  However the drag still works much more smoothly with the drag feature of the sprite turned on (I don't know why I never noticed that lock/unlock property on the sprite before) rather than doing it with code.  Thanks again for all of your help!


http://www.lsquaredmath.us/images/alienSml.jpg

Offline

 

#12 2008-07-26 22:53:57

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Formula for Sine and Cosine

lsquaredmom wrote:

I put some of the drag detection variables back in because when you are not dragging, I see no reason for the pen to keep drawing.  It makes a slight vibration that is annoying.  However the drag still works much more smoothly with the drag feature of the sprite turned on (I don't know why I never noticed that lock/unlock property on the sprite before) rather than doing it with code.  Thanks again for all of your help!

Sounds good to me...it seemed smooth enough to me on my computer.  Of course, your results may differ or maybe you are more of a perfectionist!  You are most welcome.


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#13 2008-07-28 17:32:44

S65
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Formula for Sine and Cosine

Paddle2See wrote:

While Mayhem's project admirably shows the releationship between the different trig functions, it is not the way the computer calculates them. 

I have written a project that calculates the Cosine function using the first 5 terms of the Taylor series for that function.  This is one of the types of equation that are used to actually calculate trig functions for a given angle.  Enjoy!

http://scratch.mit.edu/projects/Paddle2SeeFixIt/125979

The Taylor series is too slow for realtime use in Scratch, or maybe even a real programming language. (That's why programmers always say to precalculate sine/cosine tables.) I found a better way of doing this, which is only 1 or 2 milliseconds slower than the actual method Scratch uses, almost as accurate, and simpler:

http://scratch.mit.edu/projects/S65_Alt/227256

Thanks to this page for giving me the underlying principles.

Offline

 

#14 2008-07-29 15:14:01

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Formula for Sine and Cosine

S65 wrote:

Paddle2See wrote:

While Mayhem's project admirably shows the releationship between the different trig functions, it is not the way the computer calculates them. 

I have written a project that calculates the Cosine function using the first 5 terms of the Taylor series for that function.  This is one of the types of equation that are used to actually calculate trig functions for a given angle.  Enjoy!

http://scratch.mit.edu/projects/Paddle2SeeFixIt/125979

The Taylor series is too slow for realtime use in Scratch, or maybe even a real programming language. (That's why programmers always say to precalculate sine/cosine tables.) I found a better way of doing this, which is only 1 or 2 milliseconds slower than the actual method Scratch uses, almost as accurate, and simpler:

http://scratch.mit.edu/projects/S65_Alt/227256

Thanks to this page for giving me the underlying principles.

I would not consider 6 percent maximum error to be "almost as accurate" but I will admit that it is probably accurate enough for many gaming purposes!  Thanks for putting in the link to the reference web site; a very interesting discussion and derivation.


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#15 2008-07-29 20:58:52

S65
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Formula for Sine and Cosine

Paddle2See wrote:

I would not consider 6 percent maximum error to be "almost as accurate" but I will admit that it is probably accurate enough for many gaming purposes!  Thanks for putting in the link to the reference web site; a very interesting discussion and derivation.

Yeah, I didn't port the C code for extra precision that the site included into the project. A new version has been uploaded with that included:

http://scratch.mit.edu/projects/S65_Alt/228277

With the extra-precision added, this provides the exact same values as Scratch's. Maybe this same approximation method is used in Scratch's code too?

Offline

 

#16 2008-07-29 22:35:13

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Formula for Sine and Cosine

S65 wrote:

With the extra-precision added, this provides the exact same values as Scratch's. Maybe this same approximation method is used in Scratch's code too?

I don't think so.  If you run the online version, you get more decimal places displayed and you can see differences in the third or fourth place.  A much better approximation than the previous version!


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#17 2008-08-01 18:05:37

lenscalculator
Scratcher
Registered: 2008-08-01
Posts: 4

Re: Formula for Sine and Cosine

I need  SIN and COS for my idea as well!

I just have seen a trigonometry calculator http://scratch.mit.edu/projects/Mayhem/125960 and now inspired to create a special calculator for CCTV cameras lens calculation, to calculate a vield of view and focal length. Like in this lens calculator

I believe Scratch is enough powerful thing for it. But just need to to calculate COS and don't understand how to do it.

Last edited by lenscalculator (2008-08-01 18:07:33)

Offline

 

#18 2008-08-01 18:15:45

cctv
Scratcher
Registered: 2008-08-01
Posts: 5

Re: Formula for Sine and Cosine

Good Idea man! I need such a calc!

Offline

 

#19 2008-08-01 21:59:02

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Formula for Sine and Cosine

lenscalculator wrote:

I need  SIN and COS for my idea as well!

I just have seen a trigonometry calculator http://scratch.mit.edu/projects/Mayhem/125960 and now inspired to create a special calculator for CCTV cameras lens calculation, to calculate a vield of view and focal length. Like in this lens calculator

I believe Scratch is enough powerful thing for it. But just need to to calculate COS and don't understand how to do it.

I'm sure Scratch can handle it, if you know what the equations should be.  What part are you having trouble with?


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#20 2008-10-11 13:22:49

likegames
Scratcher
Registered: 2008-03-24
Posts: 100+

Re: Formula for Sine and Cosine

Paddle2See wrote:

lsquaredmom wrote:

I was making what I thought of as a simple triangle program based on the "trig fun" triangle program that someone else made.  The 'trig fun" program only dealt with right triangles, whereas mine is supposed to deal with all triangles.  I can't get the program to calculate anything accurate enough to get the angles to add up any where near 180 deg.  The values are just way too far off, but if I tear all of the blocks apart and click on them one at a time, they are basically doing the right thing.

Any suggestions?
The program is called: trig Fun Edited to include all triangles
and you can find it on the lsquaredmom projects page.

The basic problem here was that you were using "Broadcast" instead of "Broadcast and Wait"  Broadcast triggers scripts but does not stop execution of the remaining blocks.  By switching to "Broadcast and Wait", the speed and accuracy problems were solved.  I also took the liberty of using the new draggable sprite option and eliminating a lot of code dealing with the dragging of sprites.  My version is here:

http://scratch.mit.edu/projects/Paddle2SeeFixIt/225020

click the draggable button to drag sprites


There are 10 types of people in this world: Those who understand trinary, those who don't, and those who think this is the same old signature.
Three lines! (and base three!)

Offline

 

#21 2009-01-15 20:23:33

iphone_man
Scratcher
Registered: 2009-01-15
Posts: 1

Re: Formula for Sine and Cosine

Okay off the subject but what is abs

Offline

 

#22 2009-01-15 20:25:21

Zelda123
Scratcher
Registered: 2007-11-21
Posts: 1000+

Re: Formula for Sine and Cosine

iphone_man wrote:

Okay off the subject but what is abs

Abs is absolute value, or the distance a number is from 0 on a number line.

Offline

 

#23 2009-05-08 12:22:38

cctv
Scratcher
Registered: 2008-08-01
Posts: 5

Re: Formula for Sine and Cosine

Guys thank you.
The article from wikipedia about Cosine finnaly helped me.

Offline

 

#24 2009-10-24 15:44:58

kuv
Scratcher
Registered: 2009-06-10
Posts: 6

Re: Formula for Sine and Cosine

It is possible to use sine, cosine and tangent in scratch without the math function block.  I made a project called 'Triginometry for Experts' which doesn't use the math function block.  Please vies it.

Offline

 

#25 2009-10-24 16:13:49

kuv
Scratcher
Registered: 2009-06-10
Posts: 6

Re: Formula for Sine and Cosine

Here is a cool way to make a cannon shoot well using sine and cosine.

First you draw a cannon and a cannonball.

Then make two variables, x velocity and y velocity.


Give the cannon this script...

(When green flag clicked), ((forever) - (point towards mouse pointer.))


Give the cannonball this script...

(When green flag clicked), (set x velocity to (0)), (set y velocity to (0)), ((forever) - (Wait until (mouse down)), (point in direction(direction of cannon)), (set (x velocity) to (cosine of direction) (x10)), (set (y velocity) to (sine of direction) (x10)), (Repeat until (touching edge)) - (Change x position by (x velocity)), (Change y position by (y velocity)), (change (y velocity) by -0.2.)))

I recommend you edit these scripts until it is what you want.
It is very complicated, but well worth trying!

Offline

 

Board footer