Let's say that I have two rectangles shown on the stage. They are not sprites, but have been drawn by some sort of pen/stamp operation. What I do know is the height of each rectangle, so that I can redraw it in a different position.
Now the question: what is the quickest way available in Scratch to delete/draw a rectangle of a given (variable) height (so that I can swap them in a reasonable time?)
Thanks in advance for your help
Offline
Hello s_federici,
I tried out a method to quickly draw rectangles of a certain height.
http://scratch.mit.edu/projects/JSOlabs/199183
You can change the height of the two rectangles with two sliders,
the "wait" between drawing them can be set from 0 to 1000 (in milliseconds).
You can toggle between using a clear or not by clicking on the variable preview. (nice huh?
)
How it works:
each rectangle drawer contains 4 costumes, one with height 28, one with height 14 and one with height 1. When the sprites starts drawing, it first checks how many times it's possible to use the 'big' square costume (28x28). When the square doesn't "fits" anymore, it tries with the 28x14 rectangle. When the remaing height is less then 14, so this costume doesn't fits no longer, it switches to an horizontal line costume. (28x1). This method works very fast.
When you don't use a clear, a white costume of the rectangle's with is used to clear the space above the "new" rectangle - for in case the previous height was more than the new one. This looks very good because the screen never flashes. This makes it possible to use wait "0", but it removes any drawings in the background above the rectangles, if you would use it in your project.
When you do use a clear, the screen is cleared before each new rectangle. This does not removes background images, but clears all other pen drawings.
I recommend "Use clear? no"
Please tell me if this works good enough
Joren
Last edited by JSO (2008-06-27 11:38:29)
Offline
Thanks a lot for having created a full project as an answer to my request Joren. I think I can use it. I have only one question, out of curiosity. How did you select the 28 and 14 values?
Your method reminded me of some sort of a binary-like strategy, i.e. using rectangles that are 1, 2, 4, 8, 16, 32, 64 ... pixels high. This method would require more costumes than yours (that is exactly what I wanted to avoid, creating several hundred costumes, one for each height). But, I guess, would avoid any flickering in drawing the rectangles (as Jens pointed out in one of his last projects, Scratch loops are designed to refresh to each iteration).
I'm going to modify your project and will look what happens. In the meantime... how did you create the costumes?
Last edited by s_federici (2008-06-27 17:42:33)
Offline
The first costume I drew was just random: a horizontal line of 28px with (I used the paint grid, but I only saw this when I clicked ok in the paint editor)
Actually I first tried just to stamp each line above the previous one, however it was way to slow.
Then I got the idea of creating costumes with more height, so it would go 28 times as fast
. IT was just an experiment.
I already thought of changing the values to 2,4,8,16,32,64 etc. but I made the project really fast and I was happy with the drawing speed - especially when I tried out a method to remove the clear block.
Offline
I guess you could say I'm doing this with my Character Drawing Demo(s). I use variables to record X and Y positions, than I use the clear and stamp to mke it quickly look like it's moving. I saw JSO's game, and it looked like you kind of had the same effect, or am I just typing this in the wrong place?

Offline
The project (with binary steps) is ready at
http://scratch.mit.edu/projects/s_federici/200119.
If you download it you must see it in presentation mode to avoid flickering. And you can find the swapping project at
http://scratch.mit.edu/projects/s_federici/200123
Thanks a lot to JSO.
Offline
Hello s_federici,
I also tried out the binary system when I saw your response
However, i was not so sure about the speed.
I created costumes starting from 32x32, 32x16 up to 32x1 - which is actually not a binary value
.
I tried out the speed, by putting both drawing scripts in a forever loop, eeach changing a variable when it's ready. My old 28-14 method was *way* faster (the new was on 140, old on 200). This was probably because the script is very short.
also, you might want to remove the "change x by 0" blocks in your newest drawing project, I am sure this slows down the drawing: scratch re-draws the screen after a motion block (+/-).
Offline
JSO wrote:
I also tried out the binary system when I saw your response
. However, i was not so sure about the speed.
I compared the 28/14 version (http://scratch.mit.edu/projects/s_federici/200813) with the binary one (http://scratch.mit.edu/projects/s_federici/200815) by just adding a timer.
I first compared them for the two values height1=100 and height2=25. The binary version is about 10 times quicker (on the website; when downloaded they are both much quicker, and the binary version is only 6 times quicker that the 28/14 version).
I suppose in this case the difference in speed is mainly due to the number of stamp operations needed in the binary and 28/14 version (6 against 19 if I'm correct).
Then I compared them for two other values, height1=85 and height2=58, that are closer to the 28/14 multiples (8 against 8). So the 28/14 version should need less stamp operation. The binary version in this case is about 4 times quicker on the website.
I think the speed is due to two factors:
1. no mathematical operations such as division and rounding
2. no loops: as Jens demonstrated, animation is slower when done inside loops
The binary version has (to me) a further advantage: it just quickly swaps the two rectangles (that is what I needed). But the 28/14 (I have to admit) has a much nicer animation effect
JSO wrote:
you might want to remove the "change x by 0" blocks in your newest drawing project, I am sure this slows down the drawing: scratch re-draws the screen after a motion block (+/-).
Thanks for pointing this out. They were just there for debugging purposes.
Offline
A final (?) project to show that adding a loop would possibly make drawing faster (to be verified, as I also modified the costume mechanism, now it is double the speed of the version without loop) inevitably add the slow-motion effect
http://scratch.mit.edu/projects/s_federici/201014
See forum thread http://scratch.mit.edu/forums/viewtopic.php?id=6753 for further info.
If downloaded, see in presentation mode to avoid flickering
Last edited by s_federici (2008-06-29 11:54:25)
Offline
coolstuff wrote:
If you don't mind me asking, why exactly do you need this knewledge? A tool, game, anything...?
Sure I don't mind. I'm developping interactive visualizations of computer science classical algorithms (for example for sorting).
Offline