CIS 3020 Project 4
Introduction
In this exercise, you will use a simple string language to define shapes that will be drawn using the Turtle and Island classes. The shapes that you draw will be fractal shapes defined with a generator and interpreter that you implement.
Background
To talk about figures (or shapes), we need to define a language describing the shape we wish to draw. This language uses a string of characters to represent the actions required to draw some specific shape. One simple language definition is:
- “F” represents moving forward some distance (the face)
- “-” represents turning Right some angle (the face angle)
- “+” represents turning Left some angle (the face angle)
Assume that the face length is 10 units and that the face angle we are turning (either left or right) is 60 degrees. The string “F++F++F” defines an equilateral triangle:
- “F” – move forward 10 units,
- “+” – turn left 60 degrees,
- “+” – turn left 60 degrees,
- “F” – move forward 10 units,
- “+” – turn left 60 degrees,
- “+” – turn left 60 degrees, and
- “F” – move forward 10 units.
Fractals consist of a starting shape, called the Initiator, and a pattern, called the Generator. Replacing every side of an Initiator with the Generator creates a new shape, the next generation Initiator. For example, suppose our Initiator is the equilateral triangle defined above (“F++F++F”) and our Generator is the string “F-F++F-F”:
Initiator 0: F++F++F Generator: F-F++F-F
Replacing every “F” in Initiator 0 with the Generator creates Initiator 1 shown below. Note the use of color in Initiator 1 and the fact that Initiator 1 is scaled so its overall size is 10 units on a side (the original side length). Each group of red characters is a Generator that has replaced one “F” in Initiator 0. The black “+”s correspond to the two groups of two “+”s in Initiator 0.
Initiator 1: F-F++F-F++F-F++F-F++F-F++F-F
Subsequent generations (Initiator 2, Initiator 3, etc.) are generated in a similar manner: replacing each “F” with the Generator string.
To simplify the process of specifying and drawing a snowflake, we will use two classes, a Generator class and an InterpreterTurtle class. The Generator class will be used to create a string representing one side of the polygon that we will draw. The side will be constructed so the direction that we are facing is the same when we start and end the drawing of the side. This only occurs when the string representing a side contains an identical number of left and right turns and is reversible (it looks the same going forwards or backwards through the string) A string having these characteristics is defined to be balanced. As a result, Generator strings must be balanced. For example, the string “F-F+F+F-F” is balanced and could represent a side or Generator, while “F- F+F+F” is not and could not be used.
The InterpreterTurtle class will use the string created by the Generator class to draw the actual polygon. A method in the class (drawPolygon) will cause each side of the polygon to be drawn, turning the appropriate angle between each side.