Chapter 16
Chapter 16
KB
;.- m
\ m
North
270 West
90 East
South I_________180___________j When the turtle starts up its heading is 0. After cs the turtle has a heading of 0. At any time you can get a compass reading. Try
RT 9 0 PR H E A D I N G
o u t p u t s t h e t u r t l e s d ir e c tio n .
is part of Logos vocabulary. It is differ ent from p r i n t or f o r w a r d . It is not a com mand; it is an operation. It does not cause something to happen, but rather outputs some thing which can be used as an input. In this sec tion several other operations are introduced. The turtles position is described by two numbers which indicate how far the turtle is from the cen ter. For example, p o s i t i o n at the start is [0 0].
The first number indicates the turtles location along the horizontal or x-axis. If the turtle is west of the center, then the number will be negative. The second number indicates the turtles location along the vertical or y-axis. If the turtle is south of the center, then the number will be prefixed by a (minus sign). The turtle screen can be represented by a grid di vided into coordinates. The x coordinates run along the horizontal and the y coordinates run along the vertical. The turtle at the center has both x c o r and y c o r equal to 0. The screen dimen sions are approximately:
Logo responds
-30. 0.
________________
HOP
Now the turtle is 30 steps east of the center along the horizontal.
RT 9 0 FD 52 PR POS
The turtle is 30 steps east of the center and 52 steps north of the center.
BK PR 104 POS
Logo responds
30. -52.
The turtle is now southeast of the center 30 steps east and 52 steps south.
WRAP, FENCE, a n d WINDOW
The turtle starts out being able to w r a p ; it can walk off one edge of the screen and on at the oppo site edge. It does not change direction. Type
CS FD 5 0 0 PR POS
Logo responds
0 . 20.
Notice that the turtle is 20 steps and not 500 steps from the center.
The screen boundaries can be set up so that the turtle cannot move off the screen by typing
FENCE
Now type
CS FD 500
Logo responds
TURTLE OUT OF B OUNDS
The turtle screen will act this way until you type
FEN CE
WRAP
The command w i n d o w allows the turtle to move off the screen, but the turtle does not wrap. Thus the turtle might often be invisible to you, but still be carrying out your orders. The x and y coordi nates can be very large, cs always restores the turtle to its center position on the screen.
CS WINDOW FD 5 0 0 PR POS
W IN D O W
Logo responds
0. 500.
The turtle is now 500 steps from the center and out of view. Try using s p i after typing w i n d o w , and after typing w
f e n c e r a p.
and typing
112
I cs
FENCE SPI 5 125 CS WINDOW SPI 5 125 2
Using p o s i t i o n to Draw There is an easy way to draw a right triangle pro vided you know the lengths of the two sides join ing in the right angle. We record the starting position of the turtle. We do this by using the Logo command m a k e .
MAKE
. . ,
W INDOW
"START
POS
does two things. It puts the output from p o s in your workspace and makes s t a r t its name. Thus if you say
m a k e
iw 5 S*I
W RAP
125 2
PR
:START
and the turtle was in the center of the screen when you typed m a k e "s t a r t p o s , Logo will respond
0 . 0.
113
The turtle will be moved to :s t a r t and since the pen is down a line will be drawn. A procedure for this is
TO T R I : S I D E 1 : S I D E 2 M A K E " S T A R T POS FD : S I D E 1 RT 9 0 FD : S I D E 2 SETPOS : START END
Try
TRI 40 50 TRI 75 20
CS TRI TRI
40 75
50
20
N otice that h e a d i n g and p o s i t i o n are Logo words which cause Logo to output information. We call this kind of word an operation. You can make your own operations.
114
Logo Vocabulary The following are operations: Full Name Short Name
HEADING XCOR YCOR POSITION BACKGROUND PENCOLOR POS BG PC
Some commands that directly affect what these operations output are:
SETHEADING SETX SETY SETPOS SETBG SETPC FORWARD BACK RIGHT LEFT FD BK RT LT SETH