MSW Logo
Book Four
Rose Bay High School
Programming in 3D
Some New Commands
2D |
3D |
right (rt) |
right (rt) (on current plane) rightroll (rr) uppitch (up) |
left (lt) |
left (lt) (on current plane) leftroll (lr) downpitch (down) |
window |
perspective (enables 3d mode, in a window like mode) |
setturtle 0-1023 |
setturtle 0-1023 setturtle -1 (is your eye view, it's trajectory is which end is up) setturtle -2 (is where your looking)
|
Introduction
The beauty of these extensions is that all you have learned in 2D applies in 3D. You move the turtle like you fly an airplane. Airplanes have 3 controls to manipulate them through 3D space.
Ø Elevators on the tail.
Ø A rudder on the fin. (Uppitch/Downpitch).
Ø Ailerons on the wings. (RightRoll/LeftRoll)
MSWLogo has always had a rudder (Right/Left) also known as Yaw, you've been flying already on just 1 geometric plane in 2D. But now when we add the other 2 controls ailerons and elevators you have an infinite number of planes (geometric ones, that is) to fly on and each can be at any orientation. Once your on the desired plane you can use 2D commands to traverse it.
When you start at (X=0,Y=0,Z=0) the +Y axis is still up the screen and the +X axis is to the right of the screen. But now you can go into the screen (away from you) or out of the screen (closer to you). How, just like an airplane flies.
When an airplane Rolls to the right it does not change it's trajectory does it, neither does MSWLogo. But if you rolled 90 degrees to the right and take a right turn you will head straight down towards the ground.
Think of a real turtle on the floor. Now roll it on to its right side. Now if he takes a right turn towards his right front leg like he always does he will want to go into the floor. Let him take the turn and now his nose is pointing
at the floor. Forward will now go down in the Z axis (away from you).
Activity 1
Start MSWlogo and run the program AXIS.LGO.
To do this
1. Click on File.
2. Click on Load.
3. Click on the EXAMPLES folder.
4. Click on the 3d folder.
5. Click on the file AXIS.LGO.
6. Click Yes on the welcome window.
Activity 2
In this activity we will investigate 3D space and learn how to draw a cube.
First we will draw a square in 2D.
1. Click on File then New.
2. Click on File then Edit then OK.
3. Type the following procedure.
To
square
Repeat 4 [ FD
100 RT 90 ]
End
4. Run the procedure by typing window square in the input box of the commander window. The command WINDOW tells LOGO that we want to use 2D.
Now we will draw the square in PERSPECTIVE (3D) mode.
5. Type PERSPECTIVE in the input box to change to 3D mode.
6. Type CS to clear the screen.
7. Type square.
Now lets draw the cube.
8. Move up the wall by typing FD 100.
9. Now look down by typing DOWN 90.
10. Type square to draw another square.
11. Repeat steps 8, 9 and 10.
12. Repeat steps 8, 9 and 10.
Our program to draw a cube is now:
to square
repeat 4 [fd 100 rt
90]
end
to cube
repeat 4 [ square fd 100 down
90]
end
The program can also be written as a single procedure:
to cube2
repeat 4 [ repeat 4 [fd 100 rt 90] fd
100 down 90]
end
Activity 3
Far more complex 3D patterns can be made using simple procedures. For example:
to stack
repeat 4 [cube rt 90]
end
Your task is to write a program to produce a 3D pattern. Print out both your program and the pattern it produces. Try to use many of the new commands we have learnt. (rr lr up down etc)
HINT
You will find a great deal of help and some examples by investigating the help/index on topics such as:
Ø Circle
Ø Ellipse
Ø Ellipsearc
Ø Arc
Ø Etc
Making Solids
The following programs create solids as shown.
to doit
perspective
repeat 4 [sq fd 100 down
90]
polyview
end
to sq
polystart
repeat 4 [fd 100 rt
90]
polyend
end
to wow
cs
perspective
repeat 36[
polystart
circle 100
polyend
rr 10
]
polyview
end
Activity 4
1. Read carefully the following sections in the Help/Index:
Ø Polystart
Ø Polyend
Ø Polyview
Ø Poly Restrictions
Ø Setlight
2. Write a program to produce a ‘solid’. Print out both your program and the solid it produced.
Arrays
Arrays are a very useful way in which to manipulate data and are used in nearly all programming languages.
Activity 5
* Read carefully the topics array, item and setitem in the help/index file in MSWlogo.
Setting up an Array
The following procedure sets an array which contains three names. (the label myarray is the name of the array).
to doit
make "myarray (array 3 0)
setitem 2
:myarray "fred
setitem 1 :myarray "freddy
setitem 0 :myarray
"freda
show :myarray
show item 1 :myarray
end
{freda freddy fred}
freddy
The procedure below prompts the user to enter 10 values into the array called thisarray.
to doit2
make "i 1
make "thisarray (array 10
1)
repeat 10 [
make "j FIRST questionbox [userinput] [type a number or name]
setitem :i :thisarray :j
make "i :i+1
]
show :thisarray
end
The next procedure sets up an array and inputs the numbers from 1 to 10 into it as data.
to setup
make "i 1
make "myarray
(array 10 1)
repeat 10 [
setitem :i :myarray :i
make "i :i+1
]
show
:myarray
end
{1 2 3 4 5 6 7 8 9 10}
Activity 6
Use copy/paste to run the previous examples in MSWlogo.
Manipulating Arrays
The following examples use the array of ten consecutive numbers created by the procedure setup in the previous example.
1. Finding the total of an array.
to total
setup
make "total 0
make "i 1
repeat 10 [
make "total :total + item :i :myarray
make "i :i+1
]
show
:total
end
2. Shuffling the array.
to shuffleit
setup
shuffle 1
show :myarray
end
to shuffle :l
if :l > 100 [stop]
make "i random 10
make "j random 10
make "i
:i + 1
make "j :j + 1
make "k item :i :myarray
setitem :i :myarray item :j :myarray
setitem :j :myarray :k
shuffle :l+1
end
This program shuffles the array 100 times. To view the array you need to type show :myarray.
3. Finding the highest number in an array.
to highest
setup
make "highest
0
make "i 1
repeat 10 [
make "j item :i :myarray
if :j > :highest [make "highest :j]
make "i :i+1
]
show
:highest
end
4. Sorting an array.
to sort
repeat 9 [
make "i repcount + 1
repeat 10 [
make "j repcount
make "n item :j :myarray
make "m item :i :myarray
if :n > :m
[swap]]]
end
to swap
setitem :i :myarray :n
setitem :j :myarray :m
end
The program shown to sort arrays only works for numerical arrays (arrays of numbers).
Activity 7
Run each of the four examples under manipulating arrays and try to understand how they work. Remember you need to include the procedure setup and maximise the commander window.
Write programs to do the following:
1. A program that inputs the names of eight people and then prints out the names in random order.
(Hint: Make an array to store the names and then shuffle them).
2. A program that inputs twelve numbers from the keyboard. The program then outputs the total of the numbers, the smallest number and a list of the numbers in order.
Print out a copy of each of your programs.
A Simulation of the Spot 5 KENO Game
The following program simulates the popular KENO game where five numbers are spotted. Twenty numbers are drawn at random from eighty numbers (from 1 to 80). From the five numbers you selected the payout is:
Ø $2 for three numbers in the twenty.
Ø $19 for Four and
Ø $512 for all five.
Activity 8
Change the KENO program so that it will simulate a spot 3 Game.
In this game three numbers are spotted and the payout is:
Ø $1 for two numbers selected and
Ø $42 for all three.
Print
out a copy of your program.
to start
setup
make "payout 0
make "n1 first
questionbox [user input] [Type Your First Number]
make "n2 first
questionbox [user input] [Type Your Second Number]
make "n3 first
questionbox [user input] [Type Your Third Number]
make "n4 first
questionbox [user input] [Type Your Fourth Number]
make "n5 first
questionbox [user input] [Type Your Fifth Number]
make "games first
questionbox [user input][Number of games? ]
doit
1
end
to setup
make "i 1
make "a (array 80 1)
repeat 80 [setitem :i :a :i make "i :i+1]
end
to doit :m
shuffle 1
make "test 0
testit 1
payout
printit
(TYPE [game ]:M [.........][ Correct ] :test[/5][........]
[ Total Payout= $]:payout) print [] print[]
if :games=:m [stop]
doit :m+1
end
to shuffle :l
if :l > 500 [stop]
make "i random 80
make "j random 80
make "i :i + 1
make "j :j + 1
make "k item :i :a
setitem :i :a item :j :a
setitem :j :a :k
shuffle :l+1
endto testit :i
if :i>20 [stop]
make "k item :i :a
if :k = :n1 [make "test :test+1]
if :k = :n2 [make "test :test+1]
if :k = :n3 [make "test :test+1]
if :k = :n4 [make "test :test+1]
if :k = :n5 [make "test :test+1]
testit :i+1
endto payout
if :test=3 [make "payout :payout+2]
if :test=4 [make "payout :payout+19]
if :test=5 [make "payout :payout+512]
endto printit
make "i 1
repeat 20 [( type item :i :a [ ]) type "_ make "i :i+1]
print []
end
Activity 9
You should by now have a good grasp of MSWlogo. Your task now is to design a program of your own choice.
APPENDIX
Some More Examples
1. A Program to Print Out Prime Numbers up to 1000.
TO DOIT
type [ 2 3 5
7 9 11 ] type [ ]
PRIME
13
END
TO PRIME :X
if :X >
1000 [stop]
TESTIT :X
PRIME :X +
2
END
TO TESTIT :x
make "y int
:x/3
if
:y=:x/3 [stop]
make "y int :x/5
if :y=:x/5
[stop]
make "y int :x/7
if :y=:x/7
[stop]
make "y int :x/11
if :y=:x/11
[stop]
make "y 2
make "loop
int :x/12+1
REPEAT :loop [MAKE "A INT :X / :Y IF :A = :X / :Y [STOP] MAKE :Y :Y +
1]
type
:X print []
END
2. A Program to show that any number can be written as the sum of four square numbers.
to prob :n
make "z 0
make "a 0 make "b 0 make "c 0 make "d 0
while [:n>
:a*:a+:b*:b+:c*:c+:d*:d] [make "a
int(sqrt(:n-:z))
make "b int(sqrt(:n-:a*:a))
make "c int(sqrt(:n-:a*:a-:b*:b))
make "d int(sqrt(:n-:a*:a-:b*:b-:c*:c))
make "z :z+1 ]
type :a type
[ squared =] print :a*:a
type :b type
[ squared =] print :b*:b
type :c type
[ squared =] print :c*:c
type :d type
[ squared =] print :d*:d
print
:a*:a+:b*:b+:c*:c+:d*:d
type
[calculations=] print :z
end
To use this procedure to test the number 856 type prob 856.
Internet Resources
Rose Bay High School (IT Page) http://www.rosebay.tased.edu.au/
The Logo Foundation - http://el.www.media.mit.edu/groups/logo-foundation/
Download MSW Logo from - http://www.softronix.com
ECAWA Logo SIG - http://www.cowan.edu.au/pa/ecawa/sig/logo/logo.htm
Jim Fuller’s MSW Logo Resource Page - http://www.southwest.com.au/~jfuller/mswlogo/mswlogo.html
Lego Mindstorms Home Page - http://www.legomindstorms.com/
Logo email ‘Lists’
ECAWA Logo SIG: logo@cleo.murdoch.edu.au
To subscribe, send an email to: majordomo@cleo.murdoch.edu.au
with the words subscribe logo in the body of the email (leave the “Subject” line blank).
LOGO-L: logo-l@gsn.org
To subscribe, send and email to: majordomo@gsn.org
with the words subscribe logo-l in the body of the email (leave the “Subject” line blank).
Logo-L Archive – http://archives.gsn.org/logo-l/ - A collection of emails from the Logo-L list from January 1995