This theme is based on the fact that every calculator’s number could be drawn with the above schema:
If we want to draw a filled rectangle with dimensions 100 by 200, a first idea could be to draw a rectangle 100 by 200
then to draw a rectangle 99 by 199, then a rectangle 98 by 198 ... until the rectangle is fully filled.
Let’s begin by defining a rectangle with two variables corresponding to width and height
to rec :h :w repeat 2[fd :h rt 90 fd :w rt 90] end |
To fill our rectangle, we have to run:
rec 100 200 rec 99 199 rec 98 198 ..... rec 1 101
Let’s define a procedure for this filled rectangle.
to rectangular :h :w rec :h :w rectangular :h-1 :w-1 end |
We test rectangular 100 200 and we can see there is a problem: The procedure doesn’t stop when the rectangle has been filled, it continues infinitely! We must add a breakout test that will detect if width or height is equal to 0. When this condition is realized, we’ll ask the program to stop with the primitive stop.
to rectangular :h :w if or :h=0 :w=0 [stop] rec :h :w rectangular :h-1 :w-1 end |
Note: Instead of using the primitive or, it’s possible to use the symbol |, the line becomes:
if :h=0 | :w=0 [stop] |
to rectangular :h :w if or :h=0 :w=0 [stop] rec :h :w rectangular :h-1 :w-1 end |
We suppose that the turtle starts from the bottom left corner. We’re going to define a procedure called number
depending on 7 arguments :a, :b, :c, :d, :e, :f, :g. When :a is equal to 1, we draw the rectangle 1. If :a is equal
to 0, we don’t draw this rectangle. Here is the main idea.
The code:
to number :a :b :c :d :e :f :g # we draw the rectangular 1 if :a=1 [rectangular 160 40] # we draw the rectangular 2 if :b=1 [rectangular 40 160] penup right 90 forward 120 left 90 pendown # we draw the rectangular 3 if :c=1 [rectangular 160 40] penup forward 120 pendown # we draw the rectangular 5 if :e=1 [rectangular 160 40] # we draw the rectangular 4 left 90 penup back 40 pendown if :d=1 [rectangular 160 40] # we draw the rectangular 6 right 90 penup forward 120 left 90 pendown if :f=1 [rectangular 160 40] # we draw the rectangular 7 penup forward 120 left 90 back 40 pendown if :g=1 [rectangular 160 40] end |
to countd clearscreen hideturtle number 0 1 1 1 1 1 1 wait 60 clearscreen hideturtle number 1 1 1 1 1 1 1 wait 60 clearscreen hideturtle number 0 0 1 0 1 1 0 wait 60 clearscreen hideturtle number 1 1 1 1 0 1 1 wait 60 clearscreen hideturtle number 0 1 1 1 0 1 1 wait 60 clearscreen hideturtle number 0 0 1 1 1 0 1 wait 60 clearscreen hideturtle number 0 1 1 1 1 1 0 wait 60 clearscreen hideturtle number 1 1 0 1 1 1 0 wait 60 clearscreen hideturtle number 0 0 1 0 1 0 0 wait 60 clearscreen hideturtle number 1 1 1 0 1 1 1 wait 60 end |
Little problem: There is a flickering effect during each number drawing. To make the animation fluid, we’re going
to use the three primitives animation, stopanimation and repaint.
Here is the new code for this procedure:
to countd # Enables animation mode animation clearscreen hideturtle number 0 1 1 1 1 1 1 repaint wait 60 clearscreen hideturtle number 1 1 1 1 1 1 1 repaint wait 60 clearscreen hideturtle number 0 0 1 0 1 1 0 repaint wait 60 clearscreen hideturtle number 1 1 1 1 0 1 1 repaint wait 60 clearscreen hideturtle number 0 1 1 1 0 1 1 repaint wait 60 clearscreen hideturtle number 0 0 1 1 1 0 1 repaint wait 60 clearscreen hideturtle number 0 1 1 1 1 1 0 repaint wait 60 clearscreen hideturtle number 1 1 0 1 1 1 0 repaint wait 60 clearscreen hideturtle number 0 0 1 0 1 0 0 repaint wait 60 clearscreen hideturtle number 1 1 1 0 1 1 1 repaint wait 60 # back to classic mode stopanimation end |