Articles

racket events video 4/4 — complex events

December 3, 2019


We used to have a one barracuda timer, let’s add two more hands. We’re going to have to dive into the Dr. Racket code. We’re using picturing-program tools, we’ve got a barracuda image, a stop sign image, got a pencil for a minute hand and a arm for an hour hand. And we build our second hand by putting the barracuda on top of a clear white circle and it moves at a speed of 60 seconds for 360 degrees. Minute hand is similar, it moves 60 times 60 seconds for 360 degrees. The hour hand goes 360 degrees in 12 hours which each of those hours is 60 minutes in each of those of those is 60 seconds. We need to define a few more things, time is going to have to represent 3 hands of the clock so we define a new struct called hands with hour, minute, second hands. Time is going to just well… we’ll make a new hands object and use the hour hand, minute hand, second hand that we’ve already created those images. Update-time is going to give us a set of hands which is 1 second later than the one that it consumes. Here is how we define it. Update-time takes parameter h and make-hands.. we’ll its going to produce the rotated hour hands but we need -1 so that it moves clock-wise instead of counter-clock wise. Those move at hour hand speed which we just defined above. And it’s going to rotate the minute hand at minute hand speed, that’s also defined a little bit further above. And then the next hand we produce well we rotate the second hand we get at those by going hands second of h. Okay thats how we update time by 1 second. To render the time we need to draw the set of 3 hands, we’re going to draw clock corresponding to that define it this way, define render-time well we can just overlay the 3 hands, the second hands is simple we just go the second hand part of h for the minute hands we’ll wanna scale that a bit and for the hour hands we should also scale those a bit. Now how much to scale? Minute hand should be a but shorter than the second hand, lets say about 4/5s, and we can divide the width of the second hand by the width of the minute hand to get the right relative size. We’ll do something similar for the hour hand. Take 3/4s of the ratio between the second hand image and the hour hand image What else do we have to do to make big-bang run this? Well Remember what happens when I hit the stop down arrow, now it’s not going to produce an image, it has to produce a make-hands and those have 3 images, STOP STOP STOP We start with time thats what we are updating, on each tick of the clock we update time Also on each tick of the clock we draw what the time looks like. Lets run that. Now this is running 28 times as fast as it should just so that we can test it out. But that’s too fast for my CPU, it makes it pause every once and awhile. And it catches up again, we’ve got the 3 hands there and they seem to be moving at the correct relative speeds but theres this annoying feature where the white rectangles of the images are on obscure the image behind them we’ll need to fix that in the future, but for now lets see what happens when I press the down arrow, does it stop gracefully? Yup! There we go.

No Comments

Leave a Reply