Plotter Draws and Recursion - Project 6 | CS 1112, Study Guides, Projects, Research of Computer Science

Material Type: Project; Class: Introduction to Computing Using MATLAB; Subject: Computer Science; University: Cornell University; Term: Spring 2009;

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 08/31/2009

koofers-user-mes
koofers-user-mes šŸ‡ŗšŸ‡ø

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS1112 Spring Project 6 (Part II) due Thursday 4/30 at 11pm
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.
Objectives
Completing this part of the project will help you learn about recursion and how a plotter draws.
1 This turtle can draw!
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 hin 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
1
pf3

Partial preview of the text

Download Plotter Draws and Recursion - Project 6 | CS 1112 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

CS1112 Spring Project 6 (Part II) due Thursday 4/30 at 11pm

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.

Objectives

Completing this part of the project will help you learn about recursion and how a plotter draws.

1 This turtle can draw!

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.

2 L-systems

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.