polygons draw and fill requested?

Hey - Help? For example I really want to draw and fill a hexagon and I have no idea how to do this in Codea. I found some code on the forum that animates through a sequence of polygons which is very cool but not easy for me to decipher, hack and use, plus the polygons are outlines only and not filled. I also read that this was something that may be included in a future version of Codea - Is there still a plan to incorporate this as a feature or have I just asked a really dumb question? Any info greatly appreciated

I’ve got code to do this with meshes. I’ll share it later today.

ah perfect @Zoyt - That is real kind of you. Looking forward to checking it out later - a good way for me to learn about meshes too I suspect :wink:

Since meshes require triangles, the obvious approach is to connect all the polygon vertices to the centre point of the polygon, which breaks it into triangles.

I have a number of general tutorials on meshes
http://coolcodea.wordpress.com/

.@jasl I’m not sure if this is exactly what you’re after, but it might give you an idea. Just drag your finger around the screen in an irregular circle. It uses meshes and triangles as you move you finger around the screen and fills each triangle with the color red.


displayMode(FULLSCREEN)

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

function draw()
    background(40, 40, 50)
    m.vertices=mtab
    m:setColors(255,0,0)
    m.draw(m)
end

function touched(t)
    if t.state==MOVING then
        count = count + 1
        table.insert(tab,t)
        if count>1 then   -- create a triangle 
            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

Here’s another version of the above program. Just tap the screen to create a point. Once you create a triangle with 3 points, it will fill with red. Tap again to create another triangle with the previous one and it will fill with red.


displayMode(FULLSCREEN)

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

function draw()
    background(40, 40, 50)
    fill(255)
    for a,b in pairs(tab) do
        ellipse(b.x,b.y,6)
    end
    m.vertices=mtab
    m:setColors(255,0,0)
    m.draw(m)
end

function touched(t)
    if t.state==BEGAN then
        count = count + 1
        table.insert(tab,t)
        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

To make it simpler, I forgot it’s not a function, just a snippet of code that I use. Here it is:

meshName = mesh()
meshName.vertices = triangulate(--table of points--)
meshName:setColors(255,255,255,255)

```

The triangulate function is the key. What it does is you pass in a table of points, and it returns points for all the triangles in a mesh to create a mesh stretched across those points.  
Hope this helps.

What the!

How totally cool!

Thanks

@Ignatz - I thought the same thing when I came across it. I was Googling all these triangulation formulas, converting boat-loads of messy code, etc, then I bothered to scroll down to the bottom of the meshes reference in the documentation. I gave myself about the biggest facepalm of my life…

Here’s a program I posted long ago to show the triangulate function. The triangulate function doesn’t work well if you switch directions. Either go clockwise or counter clockwise.


-- Example of the Codea triangulate function.
-- triangulate breaks a polygon up into triangles.

function setup()
    displayMode(FULLSCREEN)
    t1={}
    t2={}   
end

function draw() 
    background(40,40,40) 
    fill(255)
    fontSize(20)
    text("Example of the Codea triangulate function.",WIDTH/2,HEIGHT-50)
    text("Keep touching the screen to create a polygon point.",WIDTH/2,HEIGHT-75)  
    for a,b in pairs(t1) do
        ellipse(b.x,b.y,2,2)
        str=string.format("(%d,%d)",b.x,b.y)
        fontSize(12)
        text(str,b.x,b.y-10)
    end
    lines()
end

function touched(t)
    if t.state==BEGAN then
        table.insert(t1,#t1+1,vec2(t.x,t.y))  
        t2=triangulate(t1)
    end
end
        
function lines()
    if t2~=nil then 
        stroke(255)
        strokeWidth(2)       
        for a=1,#t2,3 do
            line(t2[a].x,t2[a].y,t2[a+1].x,t2[a+1].y)
            line(t2[a+1].x,t2[a+1].y,t2[a+2].x,t2[a+2].y)        
            line(t2[a+2].x,t2[a+2].y,t2[a].x,t2[a].y) 
        end   
    end
end

This is all extremely useful - Thanks to everyone who posted :slight_smile:

Yeah triangulate does it right except if you use a a polygon with random vertices then it create blank spaces