Snake Illusion

An example of how to use Codea to create ‘complex’ images. The image looks like it is moving, but that is just an illusion.

-- snake illusion
-- Herwig Van Marck

function setup()
    print("Change parameters and tap image to recalculate")
    print("Set hide to 1 to go fullscreen, and tap to return")
    parameter("factor",1,10,readProjectData("factor") or 3.37)
    iparameter("init_size",50,HEIGHT,readProjectData("init_size") or 150)
    iparameter("levels",1,30,readProjectData("levels") or 18)
    iparameter("hide",0,1,0)
    hidden=0
    recalc()
end

function touched(touch)
    if (touch.state==BEGAN) then
        if (hidden==0) then
            saveProjectData("factor",factor)
            saveProjectData("init_size",init_size)
            saveProjectData("levels",levels)
            recalc()
        else
            displayMode(STANDARD)
            hidden=0
        end
    end
end

function recalc()
    img=image(init_size*13/6,init_size*13/6)
    local r=init_size
    local size=init_size/24.0
    setContext(img)
    pushMatrix()
    ellipseMode(CENTER)
    translate(init_size*13/12,init_size*13/12)
    fill(255, 255, 255, 255)
    ellipse(0,0,26*init_size/12,26*init_size/12)
    for j=1,levels do
        for i=1,36 do
            fill(0, 0, 0, 255)
            rect(-size,r-2*size,size*2,size*4)
            fill(210, 210, 99, 255)
            ellipse(-size,r,size*2,size*4)
            fill(99, 108, 210, 255)
            ellipse(size,r,size*2,size*4)
            rotate(10)
        end
        rotate(5)
        r,size=r-factor*size,size*r/(r+factor*size)
    end
    popMatrix()
    setContext()
end

-- This function gets called once every frame
function draw()
    if hide==1 then
        displayMode(FULLSCREEN_NO_BUTTONS)
        hidden=1
        hide=0
    end
    
    background(255, 255, 255, 255)

    spriteMode(CENTER)
    pushMatrix()
    for m=1,2 do
        pushMatrix()
        for l=1,math.ceil(HEIGHT/init_size) do
            pushMatrix()
            for k=1,math.ceil(WIDTH/init_size) do
                sprite(img,0,0)                
                translate(26*init_size/12,0)
            end
            popMatrix()
            translate(0,26*init_size/12)
        end
        popMatrix()
        translate(13*init_size/12,13*init_size/12)
    end
    popMatrix()
end


Nice! just tried it out. Tried to capture a picture for the forums but realized I can’t actually upload it to put in this thread.

@Simeon - Why can’t you upload a picture? Just use ![name](URL). You probably k ow that though. But this is a great example of why Codea was elected best at generative art… :smiley:

Forgot to mention it’s because I was browsing with the iPad.