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.
Offline
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)
Offline
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
Offline
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.
Offline
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.
Offline
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)
Offline
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
Offline
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.
Offline
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
Offline
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!
Offline
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!
Offline
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.
Offline
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
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/125979The 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.
Offline
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
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!
Offline
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
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?
Offline
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
Offline
Okay off the subject but what is abs
Offline
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