Saturday, October 20, 2007

Week 8: Introduction to functions

We will introduce this post to fun functions in SeeLogo and the result will be beautiful presentation for our December 10 Exhibition:

We start the session by declaring the global time variable t
(type declare t)

Our first function is P1:
New P1
jt 0 100
t=0
loop 10 [t=t+0.1 wrnum t]

The result is that 10 numbers from 0.1 to 1 will be displayed. For convenience we will always have the time t change from 0 to 1.


The next program is similar except that now 1000 numbers are displayed. This will allow smoother projections of movements on the screen that will be more pleasing to the senses.

New P1A
jt 0 100
t=0
loop 1000 [t=t+0.001 wrnum t]
jt -100 0
wrnum t

//Notice that t=t+0.001. This is not an accident since we ask that t is between 0 and 1.



The Next Program defines a function y where y=100*t (similar to y=100*x that you may be more familiar with)

New P2
local y
t=0
loop 1000 [t=t+0.001 y=100*t wrnum y]
JT -100 0
wrnum t
wrnum y



Next function is based on the Sin(x) function which you may heard about. We will explain it in detail in class: The actual function here is y=40*Sin(360*t) and it has a single period. This may seems strange at first but after a while will make sense.
NEW P3
local y
t=0
loop 100 [t=t+0.01 y=40*Sin(360*t)
fd y bk y gomv 1 0 wait 0.1]



The next program is just like the previous one but with 3 periods.
NEW P3A
local y
t=0
loop 100 [t=t+0.01 y=40*Sin(3*360*t)
fd y bk y gomv 1 0 wait 0.1]



The next program uses the definition of the Sin and Cos Functions to make a colored circle
NEW P3B
local x y
t=0
loop 100000 [t=t+0.00001
x=100*Cos(360*t)
y=100*Sin(360*t)
jt x y color rnd 512 [circle 8]]



The next two programs show what rich potential we have here. Next week we will learn about functions and colors. Once you start mastering the technique you become a Mathematical artist. Please be patient. It takes some time to get used to the new concepts but they are not difficult.

NEW P3C
local x y d
t=0
loop 100000 [t=t+0.00001
d=30*(Sin(360*3*t))^2
x=100*Cos(360*t)
y=100*Sin(360*t)
jt x y color rnd 511 [circle d]]

New P3D
local x y d r
t=0
loop 100000 [t=t+0.00001
d=30*(Sin(360*2*t))^2
r=60*(Sin(360*2*t))
x=r*Cos(360*t)
y=r*Sin(360*t)
jt x y color rnd 511 [circle d]]

1 comment:

Dani said...

Class Exercises:

put the following in the right order to make a program that prints the numbers from 1 to 10 (need to declare t first)

t=t+0.1 [ ] t=0
loop 10
wrnum 10*t

Fill in the blanks in the following program so that it will draw random colored circles using the function
y=200*x as the diameter of the circle

local y
t=0
loop 10000 [t=t+ ___ y=200*t
color rnd 512 [circle ____]]

Type NEW Try and create the precious program without copying it. Do it several time until you master it:

Contributors