

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Project; Class: Introduction to Computing Using MATLAB; Subject: Computer Science; University: Cornell University; Term: Spring 2009;
Typology: Study Guides, Projects, Research
1 / 3
This page cannot be seen from the preview
Don't miss anything!


You must work either on your own or with one partner. You may discuss background issues and general solution strategies with others, but the project you submit must be the work of just you (and your partner). If you work with a partner, you and your partner must first register as a group in CMS and then submit your work as a group.
Completing this part of the project will help you learn about recursion and how a plotter draws.
Plotters are used often in industrial graphics. The two figures below show a general plotter and a seismograph, which is a specialized plotter. A plotter has (at least) one āpenā that moves over the paper, sometimes depositing āinkā during its travel.
nxtasy.org commons.wikimedia.org
Write a function turtleDraw that draws a diagram given an instruction string. The instruction string comprises a subset of the instruction set typically used to control a plotter. This subset of instructions is often called āTurtle Graphicsā: imagine a turtle with a pen walking and sometimes drawing on a sheet of paper.
This pen-equipped turtle has a current location (x,y), a current heading h in radians, a set step length, and set clockwise and counterclockwise turn angles. In order to produce graphics, it is given instructions such as draw forward (pen down), turn clockwise, record current location, return to recorded location (pen up),... , etc. Our function has this instruction set:
āDā Draw from current position in current heading one step length.
ā+ā Turn clockwiseāchange heading without drawing.
ā-ā Turn counterclockwiseāchange heading without drawing.
ā[ā Record current location and heading and scale (reduce) the step length.
ā]ā Return to most recently recorded location and heading without drawing. Re-scale (increase) the step length.
For example, let the turtle be heading east (0 radians) and has clockwise and counterclockwise turn angle Ļ/2. Then the instruction string āD-D+D+D-Dā produces this drawing:
The instructions ā[ā and ā]ā must appear in pairs. The following example demonstrates the return to the most recently recorded position and heading as well as the scaling and re-scaling of the step length. Let the
turtle start at (0,0) heading east and have clockwise and counterclockwise turn angle Ļ/2, an initial step length of 1, and a scaling factor of 0.5. Then the instruction string āD[-DD+DDD-[+D]D-D]Dā produces this diagram. (We mark the end of each step with a circle so that it is easier to see the step size. Your final function draws lines only; do not use markers.)
0.5 1 1.5 2 2.
ā0.
0
1
How do you record the positions and headings in such a way that allows you to retrieve the most recently recorded information first? Use the idea of a stack. Starting with an empty stack, the stack size is 0 (see figure below). If you put one item in the stack, now the stack size is 1. Put another item in the stack and the stack size becomes 2. If you take out the top item the stack size decreases to 1. So all you need to do is keep track of the stack size.
stacksize=
Start with empty stack
x, y, h
stacksize=
Push in 1 item
x, y, h
x, y, h
stacksize=
Push in another item
x, y, h
stacksize=
Pop out top item
Implement this function:
function turtleDraw(inst, colr, x, y, h, len, angle, scale) % Draw a diagram given the instruction string inst using Turtle Graphics. % colr is a color name (e.g., āgā is green) or a color vector (e.g., [1 0 .5]). % x and y represent the starting coordinate of the turtle. % h is the initial heading of the turtle, in radians. % len is the step length. % angle is a 2-vector: angle(1) is how many radians to turn clockwise; % angle(2) is how many radians to turn counterclockwise. % scale, between 0 and 1, is a fraction for scaling the step length. % inst is a string containing only these characters: āDā, ā+ā, ā-ā, ā[ā, and ā]ā.
Submit your file turtleDraw.m in CMS before the deadline.
The L-system (Lindenmayer system) is used to model the growth processes of plants. Here is a diagram of an L-system āplantā after one generation and five generations of growth. You can see the pattern of the 1-generation plant all over the 5-generation plant.