133x126. The PNG itself is 3K. Running racket 5.0.2.<div><br></div><div>As an irrelevant side-note, I noticed that I had a broken trig function in there. I fixed it, but that actually had nothing to do with the performance. I also went ahead and implemented caching of the rotated images, and the performance still isn't that great, although it's far better, at least when I don't move the mouse.</div>
<div><br></div><div>y<br><br><div class="gmail_quote">On Tue, Feb 1, 2011 at 9:35 PM, Robby Findler <span dir="ltr"><<a href="mailto:robby@eecs.northwestern.edu">robby@eecs.northwestern.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
I'll leave the pedagogy questions to others, but for the performance<br>
questions, can you tell me what version you were using and the size of<br>
the png images (wxh)?<br>
<br>
Robby<br>
<div><div></div><div class="h5"><br>
On Tue, Feb 1, 2011 at 8:27 PM, Yaron Minsky <<a href="mailto:yminsky@gmail.com">yminsky@gmail.com</a>> wrote:<br>
> I am trying to teach my scratch-addicted son a bit about racket, and I'm<br>
> finding it to be rather tough going, mostly because a lot of things that are<br>
> really easy in scratch seem surprisingly hard in racket. Also, even when I<br>
> can get things done, the performance of the graphics leaves something to be<br>
> desired. I'm wondering if this is just the way the world is right now, or<br>
> if somehow I'm approaching this the wrong way.<br>
> I was trying to put together a simple animation using the universe and image<br>
> teachpacks where:<br>
> - there is a "sprite" which switches between two images periodically<br>
> - the sprite is always pointing at the mouse<br>
> I wrote some code for doing this. One problem is that getting this to work<br>
> required some actual trigonometry, which my 9 year old isn't quite ready<br>
> for. But that I could fix by writing a simple helper function. But the<br>
> other problem is that the resulting code is horribly slow. As soon as you<br>
> rotate the image, the rendering performance falls through the floor, and you<br>
> see a ton of flickering.<br>
> I've attached the code below. I can imagine some ways of optimizing this<br>
> (for example, doing the rotations only on mouse moves, and storing the<br>
> resulting rotated images in the world-struct), but I want to keep the coding<br>
> style pretty simple, and I think these kinds of optimizations get in the way<br>
> of comprehensibility.<br>
> Another thing I'd be interested in suggestions with is how to deal with<br>
> updating a struct in a clean way. Right now, you need to explicitly wrote<br>
> set-world-X functions for each field in your struct, which is pretty ugly.<br>
> Also, the fact that structs don't have field names makes them more error<br>
> prone. None of this is well suited towards building complex worlds with<br>
> lots of components. I'm wondering if there is a better way of approaching<br>
> this that doesn't require quadratic boilerplate, and that is suitable for<br>
> teaching to a kid.<br>
> Anyway, here's the code. Thanks in advance!<br>
> ;; the images here are actual PNG images, which don't show up nicely in<br>
> ascii...<br>
> (define pic1 .)<br>
> (define pic2 .)<br>
> (define (posn-diff a b)<br>
> (make-posn<br>
> (- (posn-x a) (posn-x b))<br>
> (- (posn-y a) (posn-y b))))<br>
> (define (posn-mag p)<br>
> (sqrt (+ (sqr (posn-x p)) (sqr (posn-y p)))))<br>
> (define (angle-between pos1 pos2)<br>
> (let*<br>
> ((delta (posn-diff pos2 pos1))<br>
> (hyp (posn-mag delta))<br>
> (angle-in-radians (if (= hyp 0) 0 (asin (/ (posn-y delta) hyp))))<br>
> )<br>
> (-(* angle-in-radians (/ 180 pi)))))<br>
><br>
> (define-struct world (pos mouse-pos time))<br>
> (define initial-world (make-world (make-posn 200 200) (make-posn 200 200)<br>
> 0))<br>
> (define (draw w)<br>
> (place-image<br>
> (rotate (angle-between (world-pos w) (world-mouse-pos w))<br>
> (if (is-even (round (world-time w))) pic1 pic2))<br>
> (posn-x (world-pos w))<br>
> (posn-y (world-pos w))<br>
> (empty-scene 400 400)))<br>
> (define (tick w)<br>
> (make-world<br>
> (world-pos w)<br>
> (world-mouse-pos w)<br>
> (+ 0.1 (world-time w))))<br>
> (define (mouse w x y event)<br>
> (make-world<br>
> (world-pos w)<br>
> (make-posn x y)<br>
> (world-time w)<br>
> ))<br>
> (big-bang<br>
> initial-world<br>
> (on-draw draw)<br>
> (on-tick tick)<br>
> (on-mouse mouse)<br>
> )<br>
</div></div>> _________________________________________________<br>
> For list-related administrative tasks:<br>
> <a href="http://lists.racket-lang.org/listinfo/users" target="_blank">http://lists.racket-lang.org/listinfo/users</a><br>
><br>
</blockquote></div><br></div>