[plt-scheme] HTDP exercise

 From: arnuld (arnuld3 at yahoo.co.in) Date: Sun Sep 25 06:05:59 EDT 2005 Previous message: [plt-scheme] Making a planet package Next message: [plt-scheme] HTDP exercise Messages sorted by: [date] [thread] [subject] [author]

```Hi There,
I have developed a FUNCTION named
"move-rect", which moves a rectangle across the
canvas. The function consumes a number (delta) and a
rectangle (a-rect). The function draws-and-clears the
rectangle and returns a new rectangle that has been
translated along x-axis by delta pixels. I have used
this function to move "a-rect" ONE TIME only.

PROBLEM: Can anybody tell me HOW TO Use this function
to move the rectagle several times
(say  4) across the canvas?

SINCE it is written in "DRSCHEME programming

(PLEASE do not get ANGRY, I am a 100% NEWBIE)

################ SOURCE-CODE ##################

(define-struct rect (corner width height colour))
;; DATA-DEFINITION:
;; rect is a structure where c=posn, w,h=numbers &
c=symbol
;; TEMPLATE : fun-for-rect : rect -> ???
;; (define (fun-for-rect a-rect)
;;     ..(rect-corner a-rect)...
;;      ..(rect-width a-rect)...
;;      ..(rect-height a-rect)...
;;     ..(rect-colour a-rect)...)

;; move-rect : number, rect -> rect
;; to move the given rectangle across the canvas by
delta pixels

(define a-rect (make-rect (make-posn 50 100) 40 20
'red))
(start 500 300)

(define (move-rect delta a-rect)
(cond
[(draw-and-clear-rect a-rect) (translate-rect
delta a-rect)]
[else a-rect]))

(define (translate-rect delta a-rect)
(draw-a-rect (make-rect (make-posn (+ (posn-x
(rect-corner a-rect)) delta)
(posn-y
(rect-corner a-rect)))
(rect-width a-rect)
(rect-height a-rect)
(rect-colour a-rect))))

(define (draw-and-clear-rect a-rect)
(cond
[(and (draw-a-rect a-rect) (sleep-for-a-while 2))
(clear-a-rect a-rect)]
[else a-rect]))

(define (draw-a-rect a-rect)
(draw-solid-rect (rect-corner a-rect)
(rect-width a-rect)
(rect-height a-rect)
(rect-colour a-rect)))

(define (clear-a-rect a-rect)
(clear-solid-rect (rect-corner a-rect)
(rect-width a-rect)
(rect-height a-rect)
(rect-colour a-rect)))

;; test
(move-rect 70 a-rect)

################ E. O. PROGRMME ###################

THANKS

"arnuld"

__________________________________________________________