pompon


Each of a thousand of turtles is drawing a line in a pompon. Turtles are described by a model class, which defines a handling function for the "step" signal. This function does the step forward with a small, random change of the heading and the step length.
The program creates 1000 turtles, but you can increase the number to the maximum your CPU can handle.

Take a look at the following features illustrated in the code:

  • "step" signal is sent by a dedicated turtle, it does not belong to the model class and does not draw anything; the forever loop that the turtle runs could be done also on the main turtle but it would block commands issued from the command line;
  • "Stop"/"Go!" button switches the value of shared variable :go (seen by all turtles) and allows to freeze the drawing; when :go = true the :u turtle is sending "step" signal at the maximum speed, when :go = false the :u turtle is waiting 10 milliseconds in each iteration to avoid loading the cpu with an empty loop execution;
  • "step" signal is blocking, it means that execution of the sender's code is suspended until all triggered handling functions are finished; this ensures all turtles are doing the same number of steps;
  • turtles are created simulteneously with the asynchronous constructor: (anewt $model :h :c); if you change this instruction to: (newt $model :h :c) (no "a" at the beginning) turtles will be created sequentially, which will slow down startup a lot if :n, the number of turtles, is high.

download project