Drawing project

Ive made this project but Im not sure where to go now, I could add rectangles and ellipses pretty easily, but I would like some ideas or improvements! Any criticism is welcomed. Thanks.

function setup()
    --b=Backup("MyProject Ver 1.00")
    backingMode(RETAINED)
    --phys = PhysicsDebugDraw()
    p = {}
    o = {}
    l = {}
    li = 0
    ll = {}
    time = 0
    h=0
    f=0
    id = 1
    mode = 1
    cd = 0
    dist = 5
    c = {}
    ml = {}
    vert = {}
    bodies = {}
    bugged = 0
    width = 20
    alpha = 255
    colour = color(255,255,255,255)
    bgcol = color(50,50,50)
    p6 = parameter.color("colour",colour)
    p1 = parameter.number("width",5,50,5)
    p2 = parameter.action("clear",clearScreen)
    p3 = parameter.action("undo",undo)
    p4 = parameter.color("bgcol",bgcol)
    param1 = parameter.action("Pencil",pencil)
    param2 = parameter.action("Line",Line)
    --param3 = parameter.action("getImg",getImg)
    parameter.watch("cd")
    parameter.watch("id")
     
    bgmesh = mesh()
    bgmesh:setColors(50,50,50,255)
    bgrm = bgmesh:addRect(WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
end

function table.append(t1,t2)
    local n = #t1
    for i=1, #t2 do 
        t1[n+i] = t2[i]
    end
end

function Line()
    mode = 2
    print(mode)
end

function pencil()
    mode = 1
    print(mode)
end

function clearScreen()
    p={}
    l={}
    li = 0
    ll={}
    o={}
    id=1
    ml = {}
    cd = 0
    bugged = 1
end
  
function getImg()
    setContext(dimg)
    sprt = dimg
    setContext()
end      

function undo()
    if cd > 0 and mode == 1 then
        ml[cd] = nil
        cd = cd - 1
    elseif cd == 0 and li > 0 and mode == 1 then
        ll[li] = nil
        li = li - 1
    end
    if li > 0 and mode == 2 then
        ll[li] = nil
        li = li - 1
    elseif li == 0 and cd > 0 and mode == 2 then
        ml[cd] = nil
        cd = cd - 1
    end
end

function touched(t)
    local tpos = vec2(t.x,t.y)
    local tdelta = vec2(t.deltaX,t.deltaY):normalize()
    if mode == 1 then
        if t.state == BEGAN and #p == 0 then
            bugged = 0
            cd = cd + 1
            print(cd)
            print(WIDTH)
            vert[cd] = {}
            ml[cd] = mesh()
            ml[cd]:setColors(colour)
            ml[cd].colour = colour
            p[0] = tpos
            o[0] = tpos
        elseif t.state == BEGAN and #p >= 1 then
            bugged = 1
            print("drawing bugged, restart line")
        end
        if t.state == MOVING and #p >= 0 and bugged == 0 then
            time = time + 0.1
            f=f+1
            p[id] = tpos+(tdelta):rotate(90):normalize()*(math.ceil(width))
            o[id] = tpos+(tdelta):rotate(90):normalize()*-(math.ceil(width))
            
            local tbl1 = p
            local tbl2 = o
            
            local tbl = {}
            local ntbl = {}
            h=0
            --print(id)
             while h < id do 
                --table.insert(tbl,triangulate({p[n],o[n],o[n-1],p[n-1]}))
                tbl[h] = triangulate({p[h],o[h],o[h-1],p[h-1]})
                h=h+1
            end
            --c[cd] = triangulate(p)
            local z = 0
            for a=1, #tbl do
                for b=1, #tbl[a] do
                    if tbl[a][b] then
                        z=z+1
                        --table.insert(ntbl,tbl[a][b])
                        ntbl[z] = tbl[a][b]
                    end
                end
            end
            ml[cd].vertices = ntbl
            if p[id-1] and o[id-1] and p[id] and o[id] then
                if (p[id]-p[id-1]):len() > dist and (o[id]-o[id-1]):len() > dist then
                    id = id + 1
                end
            end 
        end
        if t.state == ENDED and #p > 1 and bugged == 0 then
            if #p < 4 then id = id + 1 end
            if o[id-1] and p[id-1] then
                p[id] = o[id-1]+(p[id-1]-(p[id-2]+o[id-2])/2)
                o[id] = p[id-1]+(o[id-1]-(o[id-2]+p[id-2])/2)
            end
            
            local tbl = {}
            local ntbl = {}
            print(id)
            for n = 1, id do 
                if p[n] ~= nil and o[n] ~= nil and p[n-1] ~= nil and o[n-1] ~= nil then
                    table.insert(tbl,triangulate({p[n],o[n],o[n-1],p[n-1]}))
                end
            end
            --c[cd] = triangulate(p)
            for a=1, #tbl do
                for b=1, #tbl[a] do
                    table.insert(ntbl,tbl[a][b])
                end
            end
            ml[cd].vertices = ntbl
     
            p = {}
            o = {}
            id = 1
        end
        
        if t.state == ENDED and bugged == 1 then
            p = {}
            o = {}
            id = 1
        end
    elseif mode == 2 then
        if t.state == BEGAN then
            li = li + 1
            l[1] = tpos 
            l[2] = tpos
            if li > 1 then
                for n = 1,#ll do
                    if ll[n][1] and l[1] then
                        if n ~= li then
                            if l[1]:dist(ll[n][1]) < 20 then
                                l[1] = ll[n][1]
                            elseif l[1]:dist(ll[n][2]) < 20 then
                                l[1] = ll[n][2]
                            end
                        end
                    end
                end
            end
        end
        if t.state == MOVING and l[1] then
            if math.abs(tpos.x - l[1].x) < 10 then
                l[2] = vec2(l[1].x,tpos.y)
            elseif math.abs(tpos.y - l[1].y) < 10 then
                l[2] = vec2(tpos.x,l[1].y)
            else
                l[2] = tpos
            end
            ll[li] = {l[1],l[2],width,colour}
        end
        if t.state == ENDED then
            if li > 1 then
                for n = 1,#ll do
                    if ll[n][1] and l[2] then
                        if n ~= li then
                            if l[2]:dist(ll[n][1]) < 20 then
                                l[2] = ll[n][1]
                            elseif l[2]:dist(ll[n][2]) < 20 then
                                l[2] = ll[n][2]
                            end
                        end
                    end
                end
            end
            ll[li] = {l[1],l[2],width,colour}
            l={}
        end
    end
    --if t.state == BEGAN then print(mode) end
end

function getText(id)
    if id == 1 then
        fill(255,255,255,255)
        return "Pencil"
    elseif id == 2 then
        fill(255,255,255,255)
        return "Line"
    end
end
        
        

function draw()
    
    strokeWidth(5)
    smooth()
    lineCapMode(ROUND)
    for i = 1,#ml do
        if ml[i] then
            ml[i]:setColors(ml[i].colour)
            ml[i]:draw()
        end
    end
    for b = 1,#ll do
        local pa = ll[b][1]
        local pb = ll[b][2]
        strokeWidth(ll[b][3])
        stroke(ll[b][4])
        if pa and pb then
            line(pa.x,pa.y,pb.x,pb.y)
        end
    end
    text("Drawing Mode: "..getText(mode),100,700)
    text(math.ceil(1/DeltaTime),50,HEIGHT-50)
end

Nice project. Add a save and a load button maybe? And an eraser?

I think the Harmony code base might be something to look at, @Luatee

I started making this a month back and just thought I’d see what people thought of it. If I were to continue with it I would probably re-write it anyway, the way its written makes it harder to get at erasers and stuff like that because It’d delete the whole line but thanks, I’ll see what I can do!

Two of the early decisions in the Harmony code were:

  1. Use meshes, even for lines. Both for speed and for better look.
  2. Render to an image every so often. This then makes erasers and other effects possible.

I found No.2 out when doing what @aciolino said by looking at the main.lua in the harmony code and I thought about using lines but I was thinking I’d have to change most of the code anyway, thanks @Andrew