Is there a simple way to fill a polygon given the points of it's vertices?

Question from above ^ how can I make a triangle on the screen that’s filled in with the vertices (100,0), (200,0), (150,200)

Anyone have any simple code for it? Thanks ^.^

@Invad3rZIM Try this. Just tap the screen in 3 places to form a triangle.

EDIT: Actually, you can keep adding points that will get filled in.


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

So the secret is in meshes? Is there a simple guide to them?

There might be something that @Ignatz has in his ebooks.

Alright thanks Dave…hey why don’t you write games and publish them into apps ? I see you say you don’t do that multiple times, but you’re amazing at this…so why don’t you?

@Invad3rZIM I had 35+ years of professional programming. I’m not interested in writing anything major or even anything that takes more than an hour anymore. I’d rather just code little things that I don’t even have to think about to code. Everything I write is while I’m watch TV.

@Invad3rZim - my Codea ebook explains meshes, I think

https://www.dropbox.com/sh/mr2yzp07vffskxt/AACqVnmzpAKOkNDWENPmN4psa

Haha. Look at the function triangulate. I used it in StackIt, if you want to look at that source code. It’s quite easy to use.
Another option would be to code the shapes with shaders. It’s a bit harder, but makes it faster and easier to scale and modify on the fly.
Thanks!
P.S. I also used the triangulation in my KickAsteroids code. You can search that up on the forums too.

Here:

function polygon(verts)
    local poly = mesh()
    poly.vertices = triangulate(verts)
    poly:setColors(fill())
    poly:draw()

    for i=1, #verts - 1 do
        line(verts[i].x, verts[i].y, verts[i+1].x, verts[i+1].y)
    end
    line(verts[#verts].x, verts[#verts].y, verts[1].x, verts[1].y)
end

(this is just a slight mod of @Coder’s work)

use it like so:
polygon( vec2(0, 0), vec2(WIDTH, 0), vec2(WIDTH / 2, HEIGHT) )

@JakAttak But…the inefficiency…it makes a new mesh 60 times per second… :-S

@SkyTheCoder, haha that’s what I said. That’s why I also made a class version:

Polygon = class()

Polygon:init(verts)
    self.verts = verts

    self.poly = mesh()
    self.poly.vertices = triangulate(self.verts)
    self.poly:setColors(fill())
end

function Polygon:draw()
    self.poly:draw()

    for i=1, #self.verts - 1 do
        line(self.verts[i].x, self.verts[i].y, self.verts[i+1].x, self.verts[i+1].y)
    end
    line(self.verts[#self.verts].x, self.verts[#self.verts].y, self.verts[1].x, self.verts[1].y
end

usage: triangle = Polygon( vec2(0, 0), vec2(WIDTH, 0), vec2(WIDTH / 2, HEIGHT) )
triangle:draw()

(I was going to optimize with setContext, but it is pretty crippled in 2.0)

The user and all related content has been deleted.