We are going to write a Clojure program to implement turtle graphics. See the wi
ID: 3758933 • 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.
Explanation / Answer
Program1:
(defn cls [turtle siz]
if (> siz 2)
(do
(forward turtle (/ siz 20))
(left turtle 90) (cls turtle (* siz 0.2))
(right turtle 90)
(right turtle 90) (cls turtle (* siz 0.2))
(left turtle 90) (cls turtle (* siz 0.65))
(back turtle (/ siz 22)))))
(let [turtle (turtle 300 300)]
(pen-up turtle)
(go turtle 0 -150)
(pen-down turtle)
(pen-color turtle Color/black)
(cls turtle 1400)
(write turtle "one.png"))
Program2:
(defn moveimagebackward
[turtle distance] ; the distance of the image
(moveimageforward turtle (* -1 distance)))
(defn imageturnleft
[turtle degrees] ;degrees of the image
(update-property turtle :bearing
+ (degrees->rad degrees))) ;with degree of radians
(defn imageturnright
[turtle degrees] ;degrees of the image
(update-property turtle :bearing
- (degrees->rad degrees))) ;with degree of radians
(defn imagelowerpen
[turtle]
(set-property turtle :pen-down? true)) ; set property as pen down is true
(defn raisepenwithimage
[turtle]
(set-property turtle :pen-down? false)) ; set property as pen down is false
(defn setcoloronimage
[turtle color] ;color of pen
(set-property turtle :pen-color color)) ; set property as pen color
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.