[racket] left boundary problem

From: Roelof Wobben (r.wobben at home.nl)
Date: Thu May 10 08:02:38 EDT 2012

Hello,

I have to make a programm which displays a cat. When the cat is at the 
left or rigth end an disapear it has to turn the direction.

So I have come to this :

(define kat .)
(define workspace (empty-scene 1000 200))
(define lengte (image-width workspace))
(define lengte2 (/ (image-width kat)2))
(define ondergrens (- 0 lengte2))
(define bovengrens (+ lengte lengte2))
(define gauge-omtrek (rectangle 1000 20 "outline" "black"))

(define-struct Vcat (Xcat Hcat Richting))
; Vcat = (make-editor Number Number)
; interp. (make-editor x h) where x is the x-coordinate of the cat and h 
is the happiness of the cat.
; make-editor Number Number -> Vcat
; Vcat-Xcat Editor -> Number
; Vcat-Hcat Editor -> Number
; Vcat-Richting -> String
; Vcat? Editor Any -> Boolean


; Vcat -> Vcat
; Function which change the world on clock ticks.
; On every tick the X-coordinate changes 3 pixels and the happiness 
decrease with 0.1.
(check-expect (tock (make-Vcat -39 100 "right")) (make-Vcat -39 99.9 
"left")) ;
(check-expect (tock (make-Vcat -39 100 "left")) (make-Vcat -36 99.9 
"left")) ;
(check-expect(tock (make-Vcat 20 100 "right")) (make-Vcat 17 99.9 "right"));
(check-expect (tock (make-Vcat 20 100 "left")) (make-Vcat 23 99.9 "left")) ;
(check-expect (tock (make-Vcat 1040 100 "right")) (make-Vcat 1037 99.9 
"right"))
(check-expect (tock (make-Vcat 1040 100 "left")) (make-Vcat 1040 99.9 
"right"))
(define (tock Vcat)
   (cond
     [ (and(<= (Vcat-Xcat Vcat) ondergrens) (string=? (Vcat-Richting 
Vcat) "right")) (make-Vcat (Vcat-Xcat Vcat) (- (Vcat-Hcat Vcat) 0.1) 
"left")]
     [ (and (>= (Vcat-Xcat Vcat) -39) (<(Vcat-Xcat Vcat) bovengrens) 
(string=? (Vcat-Richting Vcat) "left")) (make-Vcat (+ (Vcat-Xcat Vcat)3) 
(-(Vcat-Hcat Vcat) 0.1) (Vcat-Richting Vcat))]
     [ (and (>= (Vcat-Xcat Vcat) ondergrens) (string=? (Vcat-Richting 
Vcat) "right")) (make-Vcat (- (Vcat-Xcat Vcat)3) (-(Vcat-Hcat Vcat) 0.1) 
(Vcat-Richting Vcat))]
     [ (and (>= (Vcat-Xcat Vcat) bovengrens) (string=? (Vcat-Richting 
Vcat) "left")) (make-Vcat (Vcat-Xcat Vcat) (-(Vcat-Hcat Vcat) 0.1) "right")]
     ))


; Vcat -> Image
; Function who display the state of the world into a image.
(check-expect (render (make-Vcat 90 100 "left")) (place-image 
(overlay/xy gauge-omtrek 0 0 (rectangle 1000 20 "solid" "red")) 500 10 
(place-image kat 90 140 workspace)))
(check-expect (render (make-Vcat 0 100 "right"))   (place-image 
(overlay/xy gauge-omtrek 0 0 (rectangle 1000 20 "solid" "red")) 500 10 
(place-image kat 0 140 workspace)))
(define (render Vcat)
    (place-image (overlay/xy gauge-omtrek 0 0 (rectangle (* 10 
(Vcat-Hcat Vcat)) 20 "solid" "red")) 500 10 (place-image kat (Vcat-Xcat 
Vcat) 140 workspace)))


(define (stop Vcat)
   (equal? (Vcat-Hcat Vcat) 0))


(define (main Vcat)
   (big-bang Vcat (on-tick tock) (on-draw render) (stop-when stop )))

(main (make-Vcat 12 10 "right"))


It works but the disadvante is that the left border is hard coded on -39.
I did that because half of the image is 37.5 so on -38 the cat has 
disappear complete.
But because the cat moves at 3 pixels and start on 0 it never gets to 
this point. The cat has as x-coordinate -36 and -39.
-38 cannot be diveded into 3.

Is there a clever way to get around this problem so I don't have to hard 
code the boundary.

Regards,

Roelof


Posted on the users mailing list.