DRAWING on surface

I posted earlier for mapping an image, I am looking for code enabling first of all to join points by lines, I am unable to perform alone my idea

And I am unable to understand your idea. What exactly do you mean with “join points by lines”? Do you want to draw a line from one point to another and then from that point to a third one and so on? Perhaps your question is really that simple but please use some more words to describe your request.

That’s what I want to do, begin on point 1, goto to an other point and join them with a line, and so on.
Is it possible to touche with to finger to announce that we want to joint the first to last point so we close the polygone. Thank you for the interest to my problem.

Without going into details, I think this little script is a starting point.

When you touch the screen you’ll see a circle there. When you touch elsewhere you’ll see another circle and a connecting line. Am I on the right track?


touchList = {}

function draw()
    local from = nil
    for i, to in ipairs(touchList) do
        ellipse(to.x, to.y, 30, 30)
        -- the first point has no predecessor to draw a line
        if from ~= nil then
            line(from.x, from.y, to.x, to.y)
        end
        -- end point is the starting point for the next line
        from = to
    end
end

function touched(t)
    if t.state == BEGAN then
        table.insert(touchList, t)
    end
end

This seems to be what you want.

About closing the polygon … can you do this as a homework?

.@Codeslinger, I didn’t initially see the line you were drawing from ‘from’ to ‘to.’ I added strokewidth(30) to the top of function draw() and it worked. @letaief may also realize the missing strokewidth(), but in case he didn’t, I thought I’d point it out.

Jeez, you’re right, I’m relying too much on loveCodea for this sort of things.

Why the hell is it that a plain line command doesn’t produce a line? Instead, we need boilerplate code when starting a project. I really have to learn this. No, better, I write a script to help me out when working on the desktop. Even better, I modify loveCodea to not draw one pixel smoothed lines.

Thanks a lot Codeslinger and Ric_Esrey for help, it works perfectly, I changed the ellipse radius to 5 and stocke to 10.
I must now find a solution to close the polygon.

@letaief

Here is a simple solution to keep the polygon closed. See code marked added.


touchList = {}

function setup()  -- added this
    displayMode(FULLSCREEN)
end

function draw()
    background(40,40,50) -- added this
    local from = nil
    for i, to in ipairs(touchList) do
        ellipse(to.x, to.y, 3, 3)
        
        -- the first point has no predecessor to draw a line
        if from ~= nil then
            strokeWidth(3)
            line(from.x, from.y, to.x, to.y)
        end
        
        -- added this   
        -- keep the polygon closed
        if i==#touchList then
            line(to.x, to.y, touchList[1].x,touchList[1].y) 
        end 
        
        -- end point is the starting point for the next line
        from = to
    end

end

function touched(t)
    if t.state == BEGAN then
        table.insert(touchList, t)
    end
end

Thanks to dave1707 for the clarity of the code.
It may be interesting to perform a continuos touch and to join first point to end point corresponding to the moment when touch stops, and to fill the enclosed area with a colour.

@letaief

Just change the word BEGAN to MOVING in the touched routine for continuous touch. As for filling in the color, that will take more code that I don’t have time for right now.

Thanks I changed it and it worked.
I think that in the future and in order to facilitate for others and contribute positively we can suggest possible uses of our ideas

  • region of interest on nuclear medcine images and count of activity
  • image clipping with irregular shapes

@letaief

Try this code for the color fill. It’s set up to do a random color for each triangle created. If you want a solid color, comment out the for loop code to get the color set by setColors(). Also, for a different effect, change MOVING to BEGAN while doing the random colors. When using BEGAN, you have to touch the screen 3 times before the first triangle shows.


function setup()
    displayMode(FULLSCREEN)
    count=0
    tab={}
    ctab={}
    mtab={}
    m=mesh()
end

function draw()
    background(40, 40, 50)
    m.vertices=mtab
    m:setColors(255,0,0) -- set color to red
    
    -- comment out the for loop code for
    -- the solid color set by m:setColors above
    for z=1,m.size do
        m:color(z,ctab[math.floor(z/3)])
    end
    
    m.draw(m)
end

function touched(t)
    if t.state==MOVING then
        count = count + 1
        table.insert(tab,t)
        table.insert(ctab,color(math.random(255),math.random(255),math.random(255)))
        if count>1 then
            table.insert(mtab,(vec2(tab[1].x,tab[1].y)))
            table.insert(mtab,(vec2(tab[count-1].x,tab[count-1].y)))
            table.insert(mtab,(vec2(tab[count].x,tab[count].y)))
        end
    end
end

I am unable to post a comment with code, it doesn’t appear as separate lines ( one paragraph ), any help !

see example below

function touched(t)
    if t.state==MOVING then
        count = count + 1
        table.insert(tab,t)
        table.insert(ctab,color(math.random(255),math.random(255),math.random(255)))
        if count>1 then
            table.insert(mtab,(vec2(tab[1].x,tab[1].y)))
            table.insert(mtab,(vec2(tab[count-1].x,tab[count-1].y)))
            table.insert(mtab,(vec2(tab[count].x,tab[count].y)))
        end
    end
end

.@letaief, It usually works if you put three tildes (i.e. this symbol ~ ) before the code and three tildes after your code.

And every now and then someone might come along and edit your post to put the tildes in (there’s only a small number of people with that ability).

Thank you for that, but what about sending a movie of the output of a program especilally when we have problems, how can that be done ?

I sincerly appreciate the dynamism of the Codea community.

Generally people upload them to youtube and then include the link here. There’s some magic that means that they automatically embed (I think you have to copy the right link, pretend to email the link to someone and I think that’s the right one).

Correct @Andrew