I made an application which draws a Sierpinski triangle. It is
constantly drawing pixels on the screen.
I use rectangles with a height and width of 1 for those pixels.
I draw them a large number (hundreds) at a time, inside image blocks,
so it isn't adding an object for every pixel, it's adding an image for
every hundred or thousand pixels.
But it'd still be nice to modify an existing Image by adding more
pixels to it, instead of adding a bunch of new images.
Is there any way to "open" an image so as to add stuff to it?
(I notice as I write this that it burns way less memory if I put each
batch of pixels in a Shape instead of an Image, so I guess I'm curious
if that is possible for Shapes either. Changing it to a Shape from an
Image.)
No biggie, it works pretty nice the way it is, if I cut it off after
it paints 10,000 pixels so that it doesn't churn forever!
Shoes.app do
def bg
background rgb(rand/2, rand/2, rand/2)
fill rgb(rand * 0.5 + 0.5, rand * 0.5 + 0.5, rand * 0.5 + 0.5, 0.5)
stroke rgb(rand * 0.5 + 0.5, rand * 0.5 + 0.5, rand * 0.5 + 0.5,
0.5)
stack do
background rgb(1.0, 1.0, 1.0, 0.5), :curve => 10
para "Click three times for a triangle, then once to start the
dots"
end
end
bg
@spot = nil
@points = []
@pixelcount = 0
animate 30 do
if @pixelcount < 10000
if @spot
shape :left => 0, :top => 0 do
300.times do
x, y = @spot
rect :left => x, :top => y, :width => 1, :height => 1
dx, dy = @points[rand(3)]
@spot = [ (x + dx) / 2, (y + dy) / 2]
end
end
@pixelcount += 300
end
end
end
click do | button, left, top |
if @spot
clear do
bg
@points, @spot, @pixelcount = [], nil, 0
end
else
if @points.length < 3
@points << [ left, top ]
else
@spot = [left, top]
end
oval :left => left, :top => top, :radius => 5, :center => true
end
end
end