Horn
WHAT IS IT?
This model is a simple demonstration of one (of many) artistic possibilities available in StarLogo. By related a turtle's motion to its color, we can easily construct interesting graphical representations of the color spectrum. In this case, the colors are arranged in the shape of a curved horn, with the lowest-valued colors (the gray shades) at the tip, and the highest-valued colors (the pink shades) at the mouth. Slides are used to specify the number of turtles, and the angle which will be swept out by the arc of the horn.
The turtle behavior for drawing the horn is extremely simple: First, a random color is assigned; then, each turtle moves out from the center, by a distance which is proportional to its color value (i.e. turtles with higher color values move further out from the center); then a new heading is assigned, again proportional to the color, with the lowest values headed "north" (a heading of 0), and the highest values headed in the direction specified by the "Sweep Angle" slider; finally, the turtle moves a set distance (the same for all turtles). The last instruction has the effect of curving the longitudinal axis of the horn along an arc of a circle.
HOW TO USE IT
Use the "Clear" button to erase any previously-drawn graphics.
Specify the number of turtles that will be used to draw the horn with the "Number of Turtles" slider.
The "Sweep Angle" slider controls the angle which will be swept out by the arc of the horn. The tip of the horn will be directly above (at a StarLogo angle of 0 degrees) the center of the arc, and the horn will be drawn clockwise from there.
Click the "Draw Horn" button to create the turtles and draw the horn.
THINGS TO NOTICE
The "Number of Turtles" slider permits a large number of turtles (10,000) to be used for drawing. However, when a turtle completes its path (based on its color), and arrives at its destination patch, it stamps the patch with the turtle color, and dies. Thus, several horns could be drawn on the screen, without clearing the graphics between each horn, and without overtaxing StarLogo with an excessive number of turtles.
Since the inner diameter of the horn increases linearly along its length (and proportionately to the color values of the turtles used to draw the horn), the circumference of the horn's cross-section increases linearly as well. This means that a similarly increasing number of turtles will be needed to provide approximately even coverage over the horn's surface. In this model, this is handled by transforming the random number distribution that generates the color values, so that higher-valued colors are more likely to be generated.
As we move from the tip to the mouth of the horn, the color values increase. Each of the 14 main colors in StarLogo has a set of shades from very dark to very light; though this spectrum is only documented for whole numbers, it is practically continuous. For example, StarLogo recognizes and displays over 80 distinct shades of red, ranging from a color value of 10.0 to a value of 19.562. At the high end of each of the shades (i.e. a basic color + 4.5 or higher), the colors are almost white. We can see this in the horn, by the 14 bands of white (or almost white) around the circumference.
Because colors are assignged randomly, and are not controlled by the order of birth (i.e. the "who" number) of each turtle, generating the 10 horns with 1000 turtles each (without using the "Clear" button after each horn is done) has almost the same effect as generating a single horn with 10,000 turtles. However, there is a critical difference: when 1000 turtles are created with a singe create-turtles command, the headings of those turtles will be spaced evenly around the 360-degree circle; i.e. the heading of one turtle will be 360/1000 degrees higher than the heading of the previous turtle. If we create another 1000 turtles, the exact same headings (and spaces between) will be used - and they will be used again and again. But if we generate 10,000 turtles at once, the headings will be spaced every 360/10,000 degrees; thus, the 10,000-turtle horn will look smoother, more evenly covered, than the results produced by generating ten 1,000-turtle horns.
EXPLORATIONS
Use the speed slider to slow the model down (you may need to set it to 10% or less, to see clearly the behavior of the turtles). Observe how the turtles with higher-numbered colors - e.g. blue, violet, magenta, pink - move further, straight out from the center, before changing direction and moving out to form the horn. In the early part of the process, even at full speed, the effect is that of the horn opening up towards the user, and then sliding off to the side.
Use different sweep angles, to see which are most effective in presenting the illusion of perspective.
Try changing the patch size to 1, and increasing the patch dimensions of the canvas; notice the difference in the texture (and speed) of the horns produced. (With a 1 pixel X 1 pixel patch size, the best results are obtained with several thousand turtles at once.) However, also note that when StarLogo saves a model, it saves the state (patch color and any model-defined patch variables) of each patch; thus, a model with more patches (whatever their size) will result in a larger file size.
STARLOGO FEATURES
As noted above, this model takes advantage of the fact that turtles created in a single create-turtles (or create-turtles-and-do) statement will have headings spaced evenly around a circle; thus, there is no need to assign random headings, as long as there is a fairly large number of turtles. (In fact, this automatic heading assignment has much better results, for the purposes of this model, than using "random 360" to assign headings, since the latter statement would only assign integral degree headings.)
Although StarLogo has built in commands to sample from the discrete uniform and normal distributions, it has no command to sample from a triangular distribution - which is what is required in this case, to bias the sample towards numbers with higher colors. Also, because we want a smooth color spectrum, we don't want to confine the model to generating only integral-valued random colors. The model uses a very straightforward computation to transform the discrete uniform distribution into a practically continuous triangular distribution.
Turtle procedures
;; Procedure: move-to-surface
;; Called by: draw-horn procedure (observer)
;;
;; First, the turtle generates a random number, using a triangular unit
;; distribution, more heavily weighted towards 0.99999 than 0.0; we can think
;; of this value as the turtle's position in the color spectrum, where 0.0
;; means that the turtle is black, and 0.99999 means that the turtle is the
;; very light pink (the highest color value that StarLogo can display). This
;; value is used to set the color, then to control the distance the turtle that
;; the turtle moves out from the center, and finally to assign a new heading.
;;
to move-to-surface
let [:index ((sqrt random 100000000) / 10000)]
setcolor (140 * :index)
forward (radius * :index)
setheading (sweep * :index)
end
;; Procedure: move-to-arc
;; Called by: draw-horn procedure (observer)
;;
;; The last step in forming the arc of the horn is for the turtle to move a set
;; distance (independent of the color) on its current heading. Having reached
;; its final destination in the horn, the turtle stamps the patch beneath it
;; with the turtle color, and dies.
;;
to move-to-arc
forward (radius * 2)
stamp color
die
end
Observer procedures
globals [
radius ; The inside radius of the horn at its widest point.
]
;; Procedure: setup
;; Called by: Setup button
;;
;; Clears turtles and the canvas.
;;
to setup
clear-all
end
;; Procedure: draw-horn
;; Called by: Draw Horn button
;;
;; Computes size of horn, creates turtles, and asks each turtle to move to its
;; position on the surface of the horn, and then to move its own tiny piece of
;; the horn into position along the final arc of the horn.
;;
to draw-horn
set radius ((min screen-half-height screen-half-width) / 4)
create-turtles-and-do num-turtles [
move-to-surface
move-to-arc
]
end
