Parabola and cosine and sine
Author |
Message |
Newguy
|
Posted: Mon May 12, 2008 8:34 pm Post subject: Parabola and cosine and sine |
|
|
I created a program that asks the user for the starting x value and y value of the parabola, the x speed , the y speed and the force of gravity and creates the parabola according to the information entered. It works ok. Then I used the search function and found this code written by another member that used cosine and sine to find the increment and created the arcing trajectory using that. I want to know how it works. Haven't done math in a while so I googled trig functions to get a quick refresher but the code still doesn't make sense to me. Below is my code followed by the code I found. I post you my code because I want to know how to create the parabola according to roots entered by the user and the vertex. I have taken grade 10 math and received above 90 % so I am good with math so I can handle most things, maybe grade 11 that you throw at me. Thanks for the help.
My code:
code: |
%Creates parabola according to user values
setscreen ("screenonly")
var x, y : real
var xspeed, yspeed : real
var gravity : real
put "Enter the starting x value (between 0 and 100 recommended)."
get x
put "Enter the starting y value (between 0 and 100 recommended)."
get y
put "Enter the x speed of the trajectory"
get xspeed
put "Enter the y speed of the trajectory"
get yspeed
put "Enter the force of gravity"
get gravity
cls
loop
x += xspeed
y += yspeed
yspeed += gravity
drawfilloval (round (x), round (y), 1, 1, red)
View.Update
end loop
|
Code posted by a user:
code: |
const GRAVITY := -0.0001
const ANGLE := 10
var x, y, xSpeed, ySpeed : real := 0
xSpeed := cosd(ANGLE)/5
ySpeed := sind(ANGLE)/5
loop
x += xSpeed
y += ySpeed
ySpeed += GRAVITY
Draw.FillOval (round (x), round (y), 1, 1, black)
end loop
|
This code I need help understanding. Especially the /5 after ANGLE.
This is how I see it and I think it is wrong. ---> cosine formula is cos(theta) = Adjacent / Hypotenuse
theta = ANGLE so 5 must be the hypotenuse right? Therefore you are trying to get adjancent size rite? Hypotenuse --> /_|
^ Adjacent
Sine formula is sin(theta) = Opposite / Hypotenuse
theta = ANGLE so 5 must be hypotenuse right? Therefore this will get the opposite size rite? Hypotenuse --> /_| <--Opposite
But what does this achieve in turing? Am I interpreting this right? I need some explanations.
I don't want to move on until I understand the code. Thanks again for help.
That ends this very long, confusing, and hard to explain post. ![Razz Razz](images/smiles/icon_razz.gif) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
CodeMonkey2000
|
Posted: Mon May 12, 2008 10:33 pm Post subject: RE:Parabola and cosine and sine |
|
|
I'm a bit iffy on how he implemented this, but here is what's going on. You have a set speed in a set angle (we call this velocity). Now when you have a velocity you need to know its x and y components to determine the coordinates of the object. If you draw the speed , it looks like this:
Basically r is the speed and theta is the angle it's going at. Now the speed in the x direction is a, and likewise b for y direction. To figure out a and b just use simple trig. However, this will cause something to go in a linear direction at angle theta. To get the curve trajectory effect you need gravity acting on your y velocity. And that's why you see the curve. If you get rid of ySpeed += GRAVITY in the code you will see a line going at 10 degrees.
And please give credit to the poster of the original code. |
|
|
|
|
![](images/spacer.gif) |
zylum
![](http://compsci.ca/v3/uploads/user_avatars/1689129126468091c334ee0.gif)
|
Posted: Tue May 13, 2008 12:17 am Post subject: RE:Parabola and cosine and sine |
|
|
Using codemonkey's image,
x = cos(theta)
y = sin(theta)
gives you the x and y components of the vector r from (px, py) to (x, y). Furthermore, the vector is normalize, meaning the length is 1. Therefore, you can multiply each component by your desired speed to get the x and y components for the desired velocity (velocity being speed with a direction). Thus the /5 just means he is assigning a speed of 1/5. |
|
|
|
|
![](images/spacer.gif) |
Newguy
|
Posted: Tue May 13, 2008 3:48 pm Post subject: Re: Parabola and cosine and sine |
|
|
The code is 4 or sumthing years old so giving credit won't do much but the code was written by do_pete_
Right now I understand how the code works and the / 5 part is the "power".
So how can I draw a parabola from the roots and the vertex? Should I use my code or the code that uses trig. |
|
|
|
|
![](images/spacer.gif) |
CodeMonkey2000
|
Posted: Tue May 13, 2008 5:17 pm Post subject: RE:Parabola and cosine and sine |
|
|
It doesn't matter. Use the one you understand best. |
|
|
|
|
![](images/spacer.gif) |
|
|