Pie Slicing
WHAT IS IT?
This model demonstrates the use of turtles to illustrate simple fractions. A fraction is specified by setting the numerator and denominator values; then, turtles arrange themselves around a portion of the perimeter of a circle (the pie), tracing out a slice of the pie in the process.
HOW IT WORKS
Each turtle uses its "who" value - comparing it to the total number of turtles, and the fraction specified by the user - to decide whether it should move from the center of the circle to the perimeter. If the turtle is the lowest- or highest-numbered turtle to move in this fashion, it sets its pen down, to trace a line on its path, thus marking the edges of the pie slice.
HOW TO USE IT
Specify the number of turtles that will be used with the "Number of Turtles" slider.
Click the "Setup" button to clear the canvas and create the turtles. (Note: the turtles will be hidden at the start.)
Use the "Numerator" and "Denominator" sliders to specify the fraction of the pie that will be sliced.
The "Fill slice?" slider controls whether the turtles will draw lines as they move from the center to the perimeter of the pie. A value of 1 indicates that the turtles will draw lines (thus, filling in the slice, to some extent), and a value of 0 indicates that only the turtles on the edges of the slice will not draw lines.
Click the "Slice Pie" button to have the turtles draw the slice of the pie equal to (or approximately so) the specified fraction. (Any previously-drawn slice will be erased first.)
After the pie is sliced, the "Fraction Specified" monitor will display the fraction given by the numerator and denominator, in decimal form (to three decimal places).
The "Turtles Selected" monitor displays the number of turtles which were moved to the edge of the pie.
The "Fraction Selected" monitor displays the fraction of the total number of turtles that "Turtles Selected" represents.
THINGS TO NOTICE
A vertical line drawn upwards from the center of the pie will always be drawn as one edge of the slice. Turtles arranged clockwise along the perimeter of the pie are those used to draw the slice; the number turtles used will also be shown in the "Turtles selected" monitor.
Notice that "Fraction Specified" and "Fraction Selected" are not always exactly the same; this usually happens when using the desired fraction of the turtles would result in the last turtle selected being a fractional turtle. Since the model can only use whole turtles, it will use the number of whole turtles that represents a fraction closest to the specified fraction.
If the specified fraction is not a proper fraction (i.e. the numerator is larger than the denominator), that represents more than an entire pie. The model can only draw one pie, so the largest fraction of turtles that will be selected is 1.00 (all the turtles).
EXPLORATIONS
Use the speed slider to slow the model down. Observe how the turtles appear and move from the center of the pie to the rim. If the slice is not being filled, most of the turtles are yellow - those on the edges of the slice are red, and drawing a red line behind them. If the slice is being filled, then all of the visible turtles are red, until they reach the edge of the pie.
Try setting the number of turtles and the fraction denominator to relatively prime values (i.e. two numbers that have no common divisors). Now, experiment with different values of the numerator; can you find any values which result in the "Fraction specified" and "Fraction selected" being equal?
Try the same experiment, but this time with the number of turtles equal to - or an integral multiple of - the denominator.
THINGS TO TRY
Experiment with different numerators, denominators, and numbers of turtles. Why is there sometimes a difference between "Fraction Specified" and "Fraction Selected"?
STARLOGO FEATURES
This model takes advantage of the fact that when a number of turtles are created (with create-turtles, create-turtles-and-do, etc.), they will be arranged with their headings evenly spaced around the compass. For example, if four turtles are created, the first will have a heading of 0, the next a heading of 90, the next a heading of 180, and the last a heading of 270. Thus, if a batch of turtles is created, and then each of them moves forward by some amount X, they will end up spaced evenly around the perimeter of a circle with radius X.
The model also takes advantage of the fact that turtles are created in order, with consecutive who numbers. Thus, in the circle described above, the turtle at the top will be turtle 0; moving clockwise, the next turtle will be 1, the next will be 2, etc. Thus, we can represent a proper fraction by computing the turtle number which corresponds to the desired fraction of the total number of turtles, and having all turtles with who values less than or equal to that number advance to the perimeter of a circle; the portion of the circumference - or the enclosed fraction of the area of the circle - traced out by moving clockwise from turtle 0 to the highest-numbered turtle on the perimeter will match (at least approximately) the original fraction.
CREDITS AND REFERENCES
This model is based on the Pie Slicing model in the original StarLogo models library; this version was executed by Nick Bennett in March 2006. The basic behavior is unchanged, except for using the rounded (instead of truncated) number of turtles to draw the pie slice, and a test to check for a whole pie being drawn, and to show turtle 0 in that event.
Turtle procedures
;; Procedure: initialize
;; Called by: setup procedure (observer) and slice procedure (turtle)
;;
;; Hides turtle (shown turtles are counted as a fractional part of the pie),
;; moves it to the origin, and lifts the drawing pen.
;;
to initialize
setcolor yellow
hideturtle
setxy 0 0
penup
end
;; Procedure: slice
;; Called by: slice-pie procedure (observer)
;;
;; Initializes turtle position, color, pen state, and shown state. If the
;; turtle's "who" value is within the desired fraction of the total turtles,
;; move forward to the perimeter of the circle (if the slice is to be filled,
;; or if the turtle is on one of the edges of the slice, put the pen down and
;; change color before moving).
;;
to slice
let [:limit (round (fraction * number-turtles))]
initialize
if (who <= :limit) [
if ((who = 0) or (who = :limit) or (fill-slice? = 1)) [
setcolor red
pendown
]
showturtle
forward (0.8 * (min screen-half-width screen-half-height))
setcolor yellow
if ((who = 0) and (number-turtles > :limit)) [
hideturtle
]
]
end
Observer procedures
globals [
fraction ; Specified fraction of pie to slice
number-turtles ; Number of turtles created
]
;; Procedure: setup
;; Called by: Setup button
;;
;; Clears turtles and the canvas, and creates new turtles.
;;
to setup
clear-all
set fraction (numerator / denominator)
set number-turtles number
create-turtles-and-do number-turtles [
initialize
]
end
;; Procedure: slice-pie
;; Called by: Slice Pie button
;;
;; Clears any previous pie slice drawn, computes the desired fraction of the pie
;; to slice, and asks the turtles to draw the new slice. Finally, waits 0.5
;; seconds, to allow monitors to "catch up"; otherwise, the model would end
;; before the monitors have a chance to compute their expressions.
;;
to slice-pie
clear-graphics
set fraction (numerator / denominator)
ask-turtles [
slice
]
wait 0.5
end
