We are going to write a Clojure program to implement turtle graphics. See the wi
ID: 3757729 • Letter: W
Question
We are going to write a Clojure program to implement turtle graphics. See the wikipedia article
on turtle graphics (
http://en.wikipedia.org/wiki/Turtle_graphics
). We will support the turtle operations moving, turning, and raising/lowering a pen to draw, undo, step and run. We will not
support changing the color of the pen, but you can if you want. These operations are described
below.
The turtle language consists of the following operations.
pen up
When the pen is up and the turtle moves nothing is drawn.
pen down
When the pen is down and the turtle moves a black line is drawn
move
The move operation requires an amount to move. This operation moves the turtle
given amount in the current direction.
turn
The turn operation requires an amount to turn. This operation turns the direction
of the turtle the given amount in degrees. You will need to covert the degrees to radians.
When running a Turtle program the result of the turtle’s pen is shown in a window. A turtle program is just a list of turtle commands.
Our Turtle program run in two modes: run and step. In the step mode the user can step the
program forward one operation by pressing the forward arrow key. The user can undo the last
operation by pressing the back arrow key. The user can undo back to the start of the turtle program. Each time the program executes an operation it prints the operation in the display window. A turtle program starts in the step mode. Pressing the “r” key changes to the run mode. In
the run mode the turtle program is executed without the user having to press the arrow keys.
When the turtle program comes to the end we switch back to step mode so the user can step
the program backwards.
Graphics
We will use the Clojure library Quil to draw in a window. See
https://github.com/quil/quil
and
http://quil.info/
. Quil does not handle menus, button and other standard GUI element. It just
handles drawing, keyboard and mouse events. Quil has several middleware functions, but do
not use them. In particular do not use fun-mode. The middleware functions maintain state for
you. Part of the assignment is to deal with maintaining state in Clojure.
What you need to do
You need to write a Clojure program that reads a turtle program from a file. Your Clojure program executes the turtle program as described above and displays the graphics in a Quil window. Don’t use an absolute path when reading the file. Put the file in the directory containing
your Clojure project and use relative paths to read the file. The sample program that you turn
in should contain at least three of each commands. Each turn command must be followed by
at least one move command. A move command must occur between every other move.
There are several issues to address. First is how to represent the turtle program. (Hint maps
are your friend.). Second is how to maintain state and keep track of where you are in the turtle
program.
Explanation / Answer
working code in clojure
(defn myprog [turtle depth]
(forward turtle 30)
(if (> depth 2)
(do
(left turtle 15)
(myprog turtle (- depth 1))
(right turtle 30)
(myprog turtle (- depth 2))
(left turtle 15)))
(back turtle 30))
(let [turtle (turtle 400 400)]
(pen-up turtle)
(go turtle 0 -100)
(pen-down turtle)
(myprog turtle 10)
(show turtle))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.