Figure out how you can paint a triangle. Write a procedure ftriangle with size and color inputs.
In this case, getting the Turtle jump inside the triangle is tricky. We know
that all angles are 60, so it would be safe to turn 30 and then jump.
ERASE "ftriangle
TO ftriangle :size :color
REPEAT 3 [FD
:size RT 120]
PU RT 30 FD :size/2 PD SETPC :color FILL
PU BK :size/2 LT 30 PD
END
CS Setbg 7 setpc
0 Ftriangle 150 4
Figure out how you can paint a circle. Write a procedure fcircle with size and color inputs.
We know that the
following procedure
allows us to draw a circle of any circumference (using the input 'c').
ERASE "circle
TO circle :c
REPEAT 360
[FD :c/360 RT 1]
END
Now, to paint the circle, the Turtle can simply turn 90 and take a jump.
The jump cannot be longer than the circle's diameter. As per our usual
strategy, we will jump half of that (which is equal to the circle's radius).
Circumference = Diameter
* 3.14159, and so,
Diameter = Circumference
/ 3.14159
ERASE
"f.circle
TO f.circle
:c :color
REPEAT 360 [FD :c/360 RT 1]
PU RT 90 FD (:c/3.14159)/2 pd setpc :color FILL
bk (:c/3.14159)/2 LT 90
END
CS Setbg 7 setpc 0 f.circle 350
14
Painting a polygon:
Write a procedure fpolygon that will draw a filled polygon. It will have two
inputs: size specifying length of
each side and N specifying type of
polygon.
In this case, getting
the Turtle jump inside the polygon is the main challenge. We know that the
smallest angle is 60 (for a triangle), so it would be safe to turn 30 and then
jump about half the length of each side.
ERASE
"fpolygon
TO fpolygon
:size :N
REPEAT :N [FD :size RT 360/:N]
PU RT 30
FD :size/2 PD FILL
PU BK :size/2
LT 30 PD
END
CS Setbg 7 setpc 11 Fpolygon 100 6
Logo Challenge:
The following program attempts to draw a circle filled green, then black,
and then green again. When it is run, the last step doesn't work: the whole
screen goes green. (Note: The screen background is black) Why is that?
ERASE "circle
TO circle :size
REPEAT 360 [FD :size/360 RT 1]
END
ERASE "greenlight
TO greenlight
SETPC 2 circle 100
SETPC 2 PU RT 90 FD 5 PD FILL BK 5 LT 90
SETPC 0 PU RT 90 FD 5 PD FILL BK 5 LT 90
SETPC 2 PU RT 90 FD 5 PD FILL BK 5 LT 90
END
CS setbg 0 setpc 7 Greenlight
Answer:
After the 2nd step (of filling with black), the entire circle
including its outline is erased, so there does not exist a circle any longer on
the screen. The last FILL, as a result, fills the nearest closed area - which
is the entire screen!
Collage of designs:
Write Logo programs to
draw the designs shown in the image below.
;First the utility procedures that we will use for all designs.
;Make the Turtle jump w.r.t. the current position
ERASE "jump
TO jump :X :Y
PU RT 90 FD :X LT 90 FD :Y PD
END
;Draw a triangle. Input: length of each side
ERASE "triangle
TO triangle :size
REPEAT 3 [FD :size RT 360/3]
END
;Draw a triangle. Input: length of each side
ERASE "ftriangle
TO ftriangle :size
triangle :size
RT 30 PU FD :size/3 PD FILL BK :size/3 LT 30
END
;Draw the polygon star pattern.
; Input: n: type of polygon,
; size: length of each side
Erase "fpstar
To fpstar :n :size
REPEAT :n [
LT 60 ftriangle :size
RT 60
FD :size
RT 360/:n
]
End
;Draw a square
ERASE "square
TO square :size
REPEAT 4 [FD :size RT 90]
END
;Draw a filled square
ERASE "fsquare
TO fsquare :size
REPEAT 4 [FD :size RT 90]
RT 45 PU FD :size/2 FILL BK :size/2 LT 45 PD
END
;---- Design 1 ----
ERASE "hexagon
TO hexagon :size
REPEAT 6 [FD :size RT 360/6]
END
ERASE "PSTAR
TO pstar :n :size
REPEAT :n [
LT 60 FD :size RT 120 FD :size LT 60
RT 360/:n
]
END
Erase "pstar.design1
To pstar.design1 :size
pstar 6 :size
hexagon :size
pu rt 60 fd :size pd setpc 12
fill pu bk :size lt 60
repeat 3 [
lt 30 fd :size/2 pd
setpc 11 fill pu bk :size/2 rt 30
FD :size RT 60 FD
:size RT 60
]
FD :size RT 60
repeat 3 [
lt 30 fd :size/2 pd
setpc 10 fill pu bk :size/2 rt 30
FD :size RT 60 FD
:size RT 60
]
pd setpc 12 hexagon :size
End
;---- Design 2: The block word FIT ----
ERASE "Bletter.F
TO Bletter.F :m
FD 100*:m RT 90 FD
60*:m
RT 90 FD 20*:m RT
90 FD 40*:m
LT 90 FD 20*:m LT
90 FD 40*:m
RT 90 FD 20*:m RT
90 FD 40*:m
LT 90 FD 40*:m RT
90 FD 20*:m RT 90
End
ERASE "Bletter.T
TO Bletter.T :m
FD 80*:m RT 90 FD
40*:m LT 90 FD 20*:m LT 90
FD 100*:m LT 90 FD
20*:m LT 90 FD 40*:m RT 90
FD 80*:m LT 90 FD
20*:m LT 90
End
ERASE "Bletter.I
TO Bletter.I :m
FD 20*:m LT 90 FD
30*:m RT 90
FD 60*:m RT 90 FD
30*:m LT 90 FD 20*:m LT 90
FD 80*:m LT 90 FD
20*:m LT 90 FD 30*:m RT 90
FD 60*:m RT 90 FD
30*:m LT 90 FD 20*:m LT 90
FD 80*:m LT 90
End
Erase "design
To design :m
setpensize round 12*:m setpc 7
bletter.F 2*:m
setpc 3 rt 45 pu fd 10*:m fill bk 10*:m lt 45 pd
jump 130*:m -250*:m
setpc 7 bletter.I 2*:m
setpc 3 lt 45 pu fd 10*:m fill bk 10*:m rt 45 pd jump 70*:m
-120*:m
jump -120*:m -140*:m
setpc 7 bletter.T 2*:m
setpc 3 lt 45 pu fd 10*:m fill bk 10*:m rt 45 pd jump 70*:m
-120*:m
end
;---- Design 3: Carrom board design ----
Erase "sqdesign
To sqdesign :o :t :c1 :c2 :c3
setpc :c1 fsquare :o
setpc :c2 repeat 4 [fsquare :t fd :o rt 90]
setpc :c3 jump :t :t fsquare :o-(2*:t)
jump -:t -:t
End
Erase "carrom.design
to carrom.design :m
jump -200*:m -200*:m sqdesign 400*:m 40*:m 13 4 1
jump 40*:m 40*:m sqdesign 320*:m 20*:m 3 4 5
jump 20*:m 20*:m fd 140*:m rt 45 sqdesign 1.414*140*:m 20*:m
3 4 12
end
;---- Design 4: House ----
;House
Erase "mywall
To mywall :size
Square :size
End
; corig is the original color
Erase "myroof
To myroof :size :corig
RT 30
setpc 8 ftriangle :size setpc :corig
LT 30
End
; corig is the original color
ERASE "mywindow
TO mywindow :SIZE :corig
REPEAT 4 [Square :size RT 90]
setpc 10
pu rt 45 fd :size/2 fill bk :size fill
fd :size/2 lt 45 pd
setpc :corig
END
; Put all parts together.
; corig is the original color
ERASE "myhouse
TO myhouse :SIZE :corig
mywall :SIZE
PU FD :SIZE/2 RT 90 FD :SIZE/2 LT 90 PD
mywindow :size/5 :corig
PU BK :SIZE/2 LT 90 FD :SIZE/2 RT 90 PD
FD :SIZE
myroof :size :corig
BK :SIZE
END
;---- Design 4: Chessboard ----
Erase "chessboard
To chessboard :m
setpc 7 fsquare 400*:m
setpc 9
jump -100*:m 0
repeat 4 [
jump 100*:m 0
repeat 4 [fsquare
50*:m jump 0 100*:m] jump 0 -400*:m
]
jump 100*:m -50*:m lt 90
repeat 4 [
jump 100*:m 0
repeat 4 [fsquare
50*:m jump 0 100*:m] jump 0 -400*:m
]
rt 90 jump -400*:m -350*:m
End
;--- Drawing all designs together in the collage format
------------
cs
jump -400 200 design 0.5
jump 150 300 setpc 7 setpensize 2 pstar.design1 75 ht
pu home jump 250 -100
carrom.design 0.75
pu home pd jump -200 -250
setpc 7 myhouse 150 7
jump 350 350 chessboard 0.5