[racket] Could this be done another way

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Mon Mar 12 14:45:30 EDT 2012

(modulo (+ position speed-for-one-step) WIDTH) 

evaluates to (+ position speed-for-one-step)  if it is less than WIDTH 
and (- (+ position speed-for-one-step)  WIDTH) if it is larger [roughly, 
but definitely in your case] 

Does this help? 


On Mar 12, 2012, at 2:25 PM, ROELOF WOBBEN wrote:

> Sorry, 
> 
> But I don't understand what the modulo and my solution work together.
> 
> Roelof
> 
> 
> On 12/03/12, Matthias Felleisen <matthias at ccs.neu.edu> wrote:
>> 
>> 
>> #lang racket
>> 
>> (require 2htdp/image 2htdp/universe)
>> 
>> ; main : CarState -> CarState
>> ; launch the program from some initial state
>> (define (main r)
>>    (big-bang 0 (on-tick tock r) (to-draw render)))
>> 
>> (define wheel-radius 5)
>> (define cab
>>   (beside (rectangle (* 6 wheel-radius)(* 4 wheel-radius) "solid" "red") 
>>           (right-triangle (* 2 wheel-radius) (* 4 wheel-radius) "solid" "red")))
>> (define body
>>   (rectangle (* 20 wheel-radius) (* 6 wheel-radius) "solid" "red"))
>> (define top
>>   (above/align "right" cab body))
>> (define tire
>>   (circle wheel-radius "solid" "black"))
>> (define bot
>>   (beside tire
>>           (rectangle (* 5 wheel-radius) (* 2 wheel-radius) "solid" "white")
>>           tire))
>> (define auto
>>   (above top bot))
>> 
>> (define WIDTH 1000)
>> (define SPEED (* 0.54 (image-width auto)))
>> (define Y-CAR 50)
>> (define BACKGROUND (empty-scene WIDTH 200))
>> 
>> 
>> ; CarState -> CarState
>> ; the clock ticked; move the car by three pixels
>> ; example:
>> ; given: 20, expected 23
>> ; given: 78, expected 81
>> (define (tock ws) 
>>   (modulo (+ ws SPEED) WIDTH)
>>   #;
>>   (if (< ws (-(image-width BACKGROUND) (* 0.54 (image-width auto))))
>>       (+ ws 3)
>>       ( - ws (image-width BACKGROUND))))
>> 
>> ; PositiveNumber -> Image
>> ; run the animation at the specified rate [1/28 is decent, try 1/100 too]
>> (define (render ws)
>>   (place-image auto ws Y-CAR BACKGROUND))
>> 
>> 
>> On Mar 12, 2012, at 10:24 AM, ROELOF WOBBEN wrote:
>> 
>>> Hello, 
>>> 
>>> I have tried to make a function which display a car running on a empty-screen.
>>> When the car  is at the end. The car has to begin at the beginning.
>>> 
>>> I have made this : 
>>> 
>>> (define wheel-radius 5)
>>> (define cab (beside (rectangle (* 6 wheel-radius)(* 4 wheel-radius) "solid" "red")(right-triangle (* 2 wheel-radius) (* 4 wheel-radius) "solid" "red")))
>>> (define body (rectangle (* 20 wheel-radius) (* 6 wheel-radius) "solid" "red"))
>>> (define top (above/align "right" cab body))
>>> (define tire (circle wheel-radius "solid" "black"))
>>> (define bot (beside tire (rectangle (* 5 wheel-radius) (* 2 wheel-radius) "solid" "white") tire))
>>> (define auto (above top bot))
>>> 
>>> (define Y-CAR 50)
>>> (define BACKGROUND (empty-scene 1000 200))
>>> 
>>> ; CarState -> CarState
>>> ; the clock ticked; move the car by three pixels
>>> ; example:
>>> ; given: 20, expected 23
>>> ; given: 78, expected 81
>>> (define (tock ws) 
>>>   (if (< ws (-(image-width BACKGROUND)(* 0.54  (image-width auto))))(+ ws 3) ( - ws (image-width BACKGROUND))))
>>> 
>>> ; CarState -> Image
>>> ; place the car into a scene, according to the given world state
>>> (define (render ws)
>>>   (place-image auto ws Y-CAR BACKGROUND))
>>> 
>>> ; main : CarState -> CarState
>>> ; launch the program from some initial state
>>> (define (main ws)
>>>    (big-bang ws (on-tick tock) (to-draw render)))
>>> 
>>> But now I wonder if I can do the same with setting the variable ws to zero.
>>> So like this : If ws is smaller then the image-width increase with the half width of the car. then ws = ws+ 3 else ws = 0 
>>> 
>>> Regards,
>>> 
>>> Roelof
>>> 
>>> 
>>> 
>>> 
>>> 
>>>                     
>>> 
>>> 
>>> 
>>> ____________________
>>>  Racket Users list:
>>>  http://lists.racket-lang.org/users
>> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120312/024f3cf9/attachment.html>

Posted on the users mailing list.