MicroWorlds index Inchworm Overview Go to the inchworm project! so you can use the project

 screenshot of inchworm (inactive buttons)

To use this project, you need the free MicroWorlds Web Player, but you do not need to know anything about MicroWorlds.

Do you know the Inchworm song from the musical, "Hans Christian Andersen"? It goes like this:
 Inchworm, inchworm, measuring the marigolds, you and your arithmetic will probably go far. Inchworm, inchworm, measuring the marigolds, seems to me you'd stop and see how beautiful they are!

In this rather silly project, every time you click the button to "measure a new flower," a different flower is drawn and the inchworm starts measuring it while the "Inchworm" tune plays. When you click the button to find out, "How long is this flower?" the inchworm will tell you. (Now, how do you suppose a cyber-inchworm measures a cyber-flower? ... in pixels, of course!)

While you could try to make a rough guess of how long the stem is before you ask the inchworm, the real interest in this project is in understanding how it works. For those of you who own the full version of MicroWorlds, the procedures are below. (Please remember that anyone can use this online project, if you download the free MicroWorlds plug-in.) Even if you don't know anything about writing programs with MicroWorlds (a multimedia version of Logo), you might like to read this brief explanation:

There are seven "turtles" in this project. Three of them are dressed as pairs of leaves. They all move to the same spot, which is chosen randomly in the grassy area. The leaves all stamp themselves there on the grass. Then one leaf turtle stays behind while the other two leaf turtles move together to a higher spot, chosen at random. A hidden turtle dressed as the flower head moves along with them and draws a green line as it moves. Now a second leaf turtle stays behind while the final leaf turtle and the hidden flower again move randomly while drawing a green line. Then the third leaf turtle stays behind while the flower turtle shows itself and once more moves randomly while drawing.

Now the inchworm turtle appears at the base of the flower to begin "measuring" it. Just before it starts measuring, the Inchworm tune begins playing, and the words to the song are shown, one line at a time. There is one textbox holding all of the words to the song (and to the inchworm's answering song), and the "song" procedure moves the cursor down from one line to the next, in time with the music.

There are actually eight different inchworm shapes. Four of them are used when the flower is fairly straight or is bent to the right, and the other four are used when the flower is bent to the left. You can control the speed of the inchworm's movements with the speed slider.

How does the inchworm know where to move? It points itself toward the first leaf turtle along the stem and moves toward it while alternating between four inchworm shapes, and when it reaches this leaf turtle, it points itself toward the next leaf turtle and keeps "crawling," and then it points itself toward the flower head and keeps crawling. Meanwhile, a command is running to tell the inchworm that when it touches the flower, it needs to turn around. And so it begins to head back down the flower, again pointing itself toward each leaf turtle in turn. There is another command running to tell it that when it reaches the lowest leaf turtle, it should turn around again. It will keep crawling up and down this leaf forever! until you click the button again or click the other button to find out what it has measured.

The computer has actually kept track of the lengths of the three randomly-drawn sections of the stem, from the moment that the marigold was drawn. So, whether or not the inchworm finishes crawling up the stem, the "inchworm" can tell you how long the stem is. The stored number is inserted into a sentence in a textbox which is then made transparent, and a "cloud" turtle appears so that it looks like a thought bubble. A hidden "bubble" turtle moves between the inchworm and the cloud, drawing a few "bubbles" along the way. (These appear as circles on a PC and as squares on a Mac.)

The sky and grass are saved as a frozen background, and when you click on "measure_a_new_flower" again, the turtles all hide themselves and the stem and bubbles are erased.

procedures for INCHWORM

Please refer to the screenshot to see the buttons, slider, textboxes, and turtles needed. There are also two hidden turtles (cloud and bubbles) and a hidden textbox called "whatlength?" There are two hidden icons for the two different melodies of the song. These melodies have been named "Inchworm,_inchworm" and "2_and_2_are_4."

* * * * * * * * * *

to startup
everyone [ht]
whatlength?, hidetext
opaque "whatlength? ct
text1, hidetext
cloud, ht
clean
end

to song
text1, top showtext
launch [inchworm,_inchworm]
repeat 2 [wait 30 cd]
wait 26 cd wait 34 cd
repeat 2 [wait 30 cd]
wait 26 cd
wait 36 cd
launch [2_and_2_are_4]
repeat 7 [wait 30 cd]
wait 40 hidetext
end

to crawl
forever [setsh "inchworm1 fd 1 + speed wait 5 - speed setsh "inchworm2 fd 1 + speed wait 4 - speed setsh "inchworm3 fd 1 + speed wait 4 - speed setsh "inchworm4 fd 1 + speed wait 5 - speed setsh "inchworm3 fd 1 + speed wait 4 - speed setsh "inchworm2 fd 1 + speed wait 4 - speed ]
end

to crawl2
forever [setsh "inchworm1a fd 1 + speed wait 5 - speed setsh "inchworm2a fd 1 + speed wait 4 - speed setsh "inchworm3a fd 1 + speed wait 4 - speed setsh "inchworm4a fd 1 + speed wait 5 - speed setsh "inchworm3a fd 1 + speed wait 4 - speed setsh "inchworm2a fd 1 + speed wait 4 - speed ]
end

to measure_a_new_flower!
cloud, ht
whatlength?, hidetext
opaque "whatlength? ct
make "total-length "0
clean
inchworm, ht
make "startingpoint -150 + random 300
tto [leaf1 leaf2 leaf3 flowerhead] pu sety -130 setx :startingpoint
tto [leaf1 leaf2 leaf3] st
ask [leaf1 leaf2 leaf3] [setsh pick [leaves1 leaves2 leaves3 leaves4 leaves5 leaves6] stamp]
pd seth 0 rt -30 + random 60
make "length 40 + random 70
fd :length
make "total-length :total-length + :length
rt -30 + random 60
make "length 40 + random 70
fd :length
make "total-length :total-length + :length
rt -30 + random 60
make "length 40 + random 70
fd :length
make "total-length :total-length + :length
inchworm, sety -130 setx :startingpoint
make "climbing? "1
st
launch [forever [song]]
[launch [crawl2] ]
[launch [crawl] ]
towards "leaf1
when [touching? "inchworm "leaf1]
[ifelse :climbing? = "1
[inchworm, towards "leaf2 ]
[inchworm, towards "leaf3 ]
]
when [touching? "inchworm "leaf2]
[ifelse :climbing? = "1
[inchworm, towards "leaf1 ]
]
[make "climbing? "0 inchworm, towards "leaf2]
when [touching? "inchworm "leaf3]
[if :climbing? = "0 [make "climbing? "1] inchworm, towards "leaf1]
end

to How_long_is_this_flower?
opaque "whatlength?
whatlength?, ct insert [This marigold is ] insert char 32 insert :total-length insert char 32
insert [ pixels long!]
transparent "whatlength?
bubbles, pu towards "inchworm fd distance "inchworm
make "howfar? 1 / 5 * distance "cloud
repeat 4 [towards "cloud rt -15 + random 30 fd :howfar? setpensize 5 + random 15 pd fd 1 pu ]
cloud, st
showtext
stopall
end