Constellations

Here’s another useless program. I call it constellations because at the start it reminds me of constellations on a star chart. When the program starts, I draw 400 random dots and then draw a line from each dot to its closest neighbor. Tap the screen and I increment the line count and connect each dot with its closest 2 neighbors. Keep tapping the screen and I increment the line count. I display the count in the upper left corner. Tap at the top of the screen to redraw another 400 random dots and connect with 1 line. As I said, it’s a useless program but kind of interesting.

displayMode(FULLSCREEN)

function setup()
    nbr=1
    lin,tab={},{}
    dots()
    lines()
end

function draw()
    background(0)
    stroke(255, 238, 0, 255)
    strokeWidth(1)
    for a,b in pairs(lin) do
        line(b.x,b.y,b.z,b.w)
    end
    noStroke()
    fill(255, 0, 0, 255)
    for a,b in pairs(tab) do
        ellipse(b.x,b.y,8)
    end
    fill(255)
    text(nbr,10,HEIGHT-20)
end

function dots()
    nbr=1
    tab={}
    for z=1,400 do
        table.insert(tab,vec2(math.random(WIDTH),math.random(HEIGHT)))
    end
    lines()
end

function lines()
    lin={}
    for a,b in pairs(tab) do
        v1=b
        dTab={}
        for z=1,nbr do
            table.insert(dTab,vec3(0,0,9999))
        end
        for c,d in pairs(tab) do
            dst=v1:dist(d)
            p=vec3(d.x,d.y,dst)
            if dst>0 then
                for z=1,#dTab do
                    if p.z<dTab[z].z then
                        t=dTab[z]
                        dTab[z]=p
                        p=t
                    end
                end
            end
        end
        for r,s in pairs(dTab) do
            table.insert(lin,vec4(b.x,b.y,s.x,s.y))
        end
    end
end

function touched(t)
    if t.state==BEGAN then
        if t.y>HEIGHT-50 then
            dots()
            return
        end
        nbr=nbr+1
        lines()
    end
end

Here’s another useless program just to waste time.

displayMode(FULLSCREEN)

function setup()
    col={}
    tab={}
    for z=1,30 do
        table.insert(col,color(math.random(255),math.random(255),math.random(255)))
        table.insert(tab,vec4(math.random(WIDTH),math.random(HEIGHT),
                math.random(-10,10),math.random(-10,10)))
    end
end

function draw()
    background(40, 40, 50)
    for a=1,#tab do
        tab[a].x=tab[a].x+tab[a].z
        if tab[a].x<0 or tab[a].x>WIDTH then
            tab[a].z=tab[a].z*-1
        end
        tab[a].y=tab[a].y+tab[a].w
        if tab[a].y<0 or tab[a].y>HEIGHT then
            tab[a].w=tab[a].w*-1
        end
        for b=a,#tab do
            stroke(col[b])
            strokeWidth(2)
            line(tab[a].x,tab[a].y,tab[b].x,tab[b].y)  
        end  
    end
end