Computer Science Canada

help to draw a swing

Author:  DAV1209 [ Tue Nov 04, 2003 4:35 pm ]
Post subject:  help to draw a swing

i have this assignment and i don't know how to do
any help plz........

Question 1. Draw a Swing

Use Turing's pixel graphics mode to draw a swing and simulate its behaviour. The sample images on the next few pages should be self-explanatory.

Your program should set the size of the screen and all parameters for the graphics should be proportional so that the program will work regardless of screen dimensions.

Your program should draw the swing at rest and tell the user what to do. Each time the user presses a key the swing should move a small distance away from the rest position. Then, when the user presses the SPACEBAR the swing will be released and swing back and forth. You will need to control the speed of the swing so that the swinging is visible to the user.

The swing should travel a shorter distance each time it changes direction so that it will stop back at the rest position. You will further need to control the speed to make the swing slow down as its arc gets shorter. It is not necessary that you model the actual motion of a swing, though you can if you feel up to it.

To "freeze" the swing during its motion click the Pause button in the Run Window. This will allow you print it at different positions. The Print button will send the contents of the Run window directly to the printer - without the Title Bar and Menu Bar.

Author:  Tony [ Tue Nov 04, 2003 4:45 pm ]
Post subject: 

sounds simple enough..

gravity is constant = -9.81 m/s^2
the velocity is in direction of a tangent of the swing's path.

Some physics and some calculus and you'll be modeling simulations in no time Laughing

Though if accurasy is not required, you can just have some forloops Rolling Eyes The initial size of the loop depends on the distance swing is pulled back, and it diminishes with each swing because of friction.

Author:  DAV1209 [ Wed Nov 05, 2003 7:22 pm ]
Post subject: 

it might sound easy for you but not for me
i just start my first year in CS in York and i have no experience at programming
and in my turing couse, my professor is just doing some funny things in class
he didn't show us or help us on the assignment and also i'm a international student so i have some difficulties with this assignment and i dont' know anyone to ask for help.
can you show me more careful to write this program
the file i attach with is the example of how the output is
thanks

Author:  Tony [ Thu Nov 06, 2003 10:14 am ]
Post subject: 

they teach turing at york?! at York University?! Shocked
*spontaniously combusts*
Your prof is on crack.

But dont worry, we'll help you here Very Happy

First of all... you have to keep in mind that this assignment is an animation, not an actual stimulation, so we don't need exact numbers. We can assume and create perfect conditions to make it easier for us Laughing

First of all.. the input. Hold space down to pull swing back.
code:

var pull:int:=0
put "hold down space to pull back"
loop
if hasch then %if input buffer has length
     loop
     pull+=1 %adds one to pull
     locate(1,1)
     put "swing is pulled back:" + pull
     exit when not hasch %exit when buffer is empty
     end loop
end if
exit when pull>0 %swing was pulled back, exit loop
end loop


I dont have turing installed on my school computer, can't say how acurate that code is, but that's the idea. Using Input.KeyDown(read tutorial) will probably be more efficient and accurate though, since if your keyboard's repeat rate is too slow or too fast, you might run into issues.

Now for the swing itself... We just go in a forloop, back and forth, gradually losing the number of degrees. We calculate the position of the swing using Trig (Cos/Sin)

code:

%after input is taken
var pull:int := 180 %is how many degrees it swings, from input above
loop
exit when pull<=0 %exit loop when swing stops
for i:pull .. -pull+5 %loses 5 degrees per swing. Change +5 to +1 later
Draw.FillOval(100+sind(i)*100,100+cosd(i)*100,5,5,red)
%draws red circle for a swing. Circle hangs from 100,100 and rope length is 100
Draw.Line(100,100,sind(i)*100,cosd(i)*100,black) %draws line
end for

%now to go back
for i:-pull+5 .. pull -10
Draw.FillOval(100+sind(i)*100,100+cosd(i)*100,5,5,red)
Draw.Line(100,100,sind(i)*100,cosd(i)*100,black)
end for

pull := pull - 10 %we lost 10 degrees while swinging back and forth

end loop


The code above might make the swing go upside down... and it probably will. To fix it, just change +cosd(i) and +sind(i) to -cosd(i) and -sind(i)

I'll check on accurasy of the code when I get home.

Author:  Mazer [ Thu Nov 06, 2003 2:12 pm ]
Post subject: 

i thought they taught fortran at york. that's what my friend is being taught, anyways.

Author:  DAV1209 [ Thu Nov 06, 2003 3:51 pm ]
Post subject: 

yeah they did teach turing in York university
my major is computer sience and turing course is the course for non programming experience student

and right on my first programming course i have this professor
what a pain in the ass
he didn't even know how to use windows
i don't know how did he be a professor in York university
but beside he's kinda easy with us
he even answer the question when we doing the test and give some extra mark for student
he always ask what did he teach and what he did not Sad Sad
anyway i'm gonna try the turing code you show me to see if it's work
thank you so much Tony

hey Tony i saw your name is Tony Dan
are you a vietnamese ?
Mazerm, in York they have so many courses for you to chouse and Turing is my first programming course
your friend might chose the different course that teach fortran

Author:  DAV1209 [ Thu Nov 06, 2003 3:53 pm ]
Post subject: 

by the way this the link for download turing i found on the forum


[url]http://schools.tdsb.on.ca/dmthomson/CS/Turing/401/SETUP.EXE [/url][/url]

Author:  DAV1209 [ Thu Nov 06, 2003 4:18 pm ]
Post subject: 

i try to run the program but there is some errors and i couldn't find the way to fix it

Author:  Dan [ Thu Nov 06, 2003 6:03 pm ]
Post subject: 

"Tony Dan", lol

no Dan is not Tony's last name it is my 1st name Razz

i am the admin that posts less but keeps it running (also the one who gets blamed for everything Evil or Very Mad )

Author:  DAV1209 [ Thu Nov 06, 2003 7:27 pm ]
Post subject: 

lol i saw the name Dan and i thought it was Tony's name but ....
hey Dan, your name is short for Daniel or what?
coz Dan is vietnamese name too
just wondering if you are vietnamese or not

Tony
i checked through the program , there are some errors but i couldn't fix it

Author:  thoughtful [ Thu Nov 06, 2003 8:01 pm ]
Post subject: 

LOL, man this is funny turing at York. My friend asked me to make a similar program for his sister who studies at York. I couldn't beleive him, i thought he was screwing around. Very Happy Very Happy Very Happy

Author:  thoughtful [ Thu Nov 06, 2003 8:03 pm ]
Post subject: 

And bythe way the program tony made doesnt work because he didn't round the values for the drawing. Maybe he thought that you were atleast that smart Razz Razz Razz

Author:  Tony [ Thu Nov 06, 2003 8:15 pm ]
Post subject: 

pff Rolling Eyes come on guys, give me a break. just 2.5 mistakes in the code. and I didn't even had the compiler to check it. Was writing it up right here.

1 - forgot to round (Java and C++ automatically typecast Very Happy)
2 - first loop should be decreasing since it goes backwards... once again, Java/C++'s forloops pwn
2.5 - swing was upside down, but I mentioned that when I wrote up the code

here's the fixed one
code:

%after input is taken
var pull:int := 45 %is how many degrees it swings, from input above
loop
exit when pull<=0 %exit loop when swing stops
for decreasing i:pull .. -pull+5 %loses 5 degrees per swing. Change +5 to +1 later
Draw.FillOval(100-round(sind(i)*100),100-round(cosd(i)*100),5,5,red)
%draws red circle for a swing. Circle hangs from 100,100 and rope length is 100
Draw.Line(100,100,100-round(sind(i)*100),100-round(cosd(i)*100),black) %draws line
delay(10)
cls
end for

%now to go back
for i:-pull+5 .. pull -10
Draw.FillOval(100-round(sind(i)*100),100-round(cosd(i)*100),5,5,red)
%draws red circle for a swing. Circle hangs from 100,100 and rope length is 100
Draw.Line(100,100,100-round(sind(i)*100),100-round(cosd(i)*100),black) %draws line
delay(10)
cls
end for

pull := pull - 10 %we lost 10 degrees while swinging back and forth

end loop


btw, DAV1209 - I come from Russian background if you care 8)

Author:  Dan [ Fri Nov 07, 2003 5:10 pm ]
Post subject: 

DAV1209 wrote:

hey Dan, your name is short for Daniel or what?
coz Dan is vietnamese name too
just wondering if you are vietnamese or not


Short for Daniel

Author:  DAV1209 [ Tue Nov 11, 2003 5:44 pm ]
Post subject: 

thanks you so much Tony but when i put the 2 codes together
the program still not working
code:

var pull:int:=0
put "hold down space to pull back"
loop
if hasch then %if input buffer has length
     loop
     pull+=1 %adds one to pull
     locate(1,1)
     put "swing is pulled back:" + pull
     exit when not hasch %exit when buffer is empty
     end loop
end if
exit when pull>0 %swing was pulled back, exit loop
end loop
%after input is taken
var pull:int := 45 %is how many degrees it swings, from input above
loop
exit when pull<=0 %exit loop when swing stops
for decreasing i:pull .. -pull+5 %loses 5 degrees per swing. Change +5 to +1 later
Draw.FillOval(100-round(sind(i)*100),100-round(cosd(i)*100),5,5,red)
%draws red circle for a swing. Circle hangs from 100,100 and rope length is 100
Draw.Line(100,100,100-round(sind(i)*100),100-round(cosd(i)*100),black) %draws line
delay(10)
cls
end for

%now to go back
for i:-pull+5 .. pull -10
Draw.FillOval(100-round(sind(i)*100),100-round(cosd(i)*100),5,5,red)
%draws red circle for a swing. Circle hangs from 100,100 and rope length is 100
Draw.Line(100,100,100-round(sind(i)*100),100-round(cosd(i)*100),black) %draws line
delay(10)
cls
end for

pull := pull - 10 %we lost 10 degrees while swinging back and forth

end loop



i did try to take out this line
Quote:
var pull:int := 45 %is how many degrees it swings, from input above
because the first possition of the line and ball is in the middle when ever we hit the space bar button, the ball goes up a little bit and the ball will release and starting falling down.
and another problem is
in this statement
Quote:
put "swing is pulled back:" + pull
,
i try to change to + round(pull) but it's still cause an error

Author:  Tony [ Tue Nov 11, 2003 7:15 pm ]
Post subject: 

well you were right about removing that over var pull line. It is not needed there.

Anyways, the pulling back part is buggy. Confused The simpliest way ofcourse is to replace top part with
code:

var pull:int
put "how many degrees would you like to pull your swing back?"
get pull


Laughing Otherwise I donno... you can use getch I suppose Confused

Author:  DAV1209 [ Wed Nov 12, 2003 8:39 pm ]
Post subject: 

but my assignment requires me when ever i hit the space bar button, the ball go up 1 degree and when ever i hit another button. The ball starting falling down at that time
it's not requires me to input the degree

Author:  Chimaera [ Wed Nov 12, 2003 9:04 pm ]
Post subject: 

So you just use getch statements and if statements. Very easy

code:

getch key
if key = "a" then
angle := angle +1 %this isn't really the actual code to make it go up and down but it simulates what's actually supposed to be there
elsif key = "s" then
angle:= angle -1 %ditto
end if


Btw you might want to specify whether or not if you hit a button and it continuously keeps falling down or going up until a certain point or another button is it or whether it just moves a small bit then stops.

Author:  Tony [ Wed Nov 12, 2003 11:54 pm ]
Post subject: 

Chimaera has the right idea.

code:

var angle:int := 0
var c:string(1)

loop
getch(c)
if c=" " then
angle +=1
else
exit
end if
end loop

put angle

Author:  DAV1209 [ Thu Nov 13, 2003 1:31 am ]
Post subject: 

i did put it together and it work fine
but when i press space i didn't see the ball go up
how to draw the ball right at time we start the program and when ever we hit the space bar button, the ball go up 1 degree and we still can see it go up
the file i attached with showed how the out put of the program

code:


var angle:int := 0
var c:string(1)

loop
getch(c)
if c=" " then
angle +=1
else
exit
end if
end loop

put angle


loop
exit when angle<=0
for decreasing i:angle .. -angle+5
Draw.FillOval(200-round(sind(i)*100),200-round(cosd(i)*100),5,5,red)

Draw.Line(200,200,200-round(sind(i)*100),200-round(cosd(i)*100),black)
delay(20)
cls
end for

for i:-angle+5 .. angle -10
Draw.FillOval(200-round(sind(i)*100),200-round(cosd(i)*100),5,5,red)

Draw.Line(200,200,200-round(sind(i)*100),200-round(cosd(i)*100),black)
delay(20)
cls
end for

angle := angle - 5

end loop

Author:  DAV1209 [ Thu Nov 13, 2003 6:30 am ]
Post subject: 

lol...
thanks you guy all for helping me on the assignment

i've just finished my work
here is the finished code
code:


var angle : int := 0
var space : string (1)

loop
drawline (200, 200, 100, 100, black)
drawline (200, 200, 300, 100, black)
Draw.FillOval (200 - round (sind (angle) * 100), 200 - round (cosd (angle) * 100), 5, 5, red)
Draw.Line (200, 200, 200 - round (sind (angle) * 100), 200 - round (cosd (angle) * 100), black)
put "Press Space Bar to push the swing, press another key to release the swing",
    getch (space)
    if space = " " then
        angle += 1
        cls
    else
        exit
    end if
end loop
put angle


loop
    exit when angle <= 0
    for decreasing i : angle .. -angle + 5
        drawline (200, 200, 100, 100, black)
        drawline (200, 200, 300, 100, black)

        Draw.FillOval (200 - round (sind (i) * 100), 200 - round (cosd (i) * 100), 5, 5, red)
        Draw.Line (200, 200, 200 - round (sind (i) * 100), 200 - round (cosd (i) * 100), black)
        delay (30)
        cls
    end for

    for i : -angle + 5 .. angle - 10
        drawline (200, 200, 100, 100, black)
        drawline (200, 200, 300, 100, black)

        Draw.FillOval (200 - round (sind (i) * 100), 200 - round (cosd (i) * 100), 5, 5, red)
        Draw.Line (200, 200, 200 - round (sind (i) * 100), 200 - round (cosd (i) * 100), black)
        delay (30)
        cls
    end for

    angle := angle - 5

end loop

Author:  DAV1209 [ Thu Nov 13, 2003 6:58 am ]
Post subject: 

i don't know what happen but the program just work but i don't know why i run the program again it cause a new error
on the getch statement
Crying or Very sad Crying or Very sad

Author:  DAV1209 [ Thu Nov 13, 2003 7:13 am ]
Post subject: 

i found the problem on my code
i have to take the
Quote:

put "Press Space Bar to push the swing, press another key to release the swing",

statement out
but i don't know why i can't put that put statement there

Author:  Tony [ Thu Nov 13, 2003 10:10 am ]
Post subject: 

that comma (,) must be causing problems with your input.


: