Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

'Other' Shapes in Codea

I was wondering if it was possible to make shapes that are not rectangles or circles in Codea. I know you can make triangles useing meshes but what about quadrilaterals, pentagons, hexagons, etc.

Tagged:

Comments

  • edited March 2015 Posts: 216

    All those can be made with triangles
    Edit: I think there is a triangulate function that converts a set of vertices into triangles, but I don't know much about it (or if it exists)

  • IgnatzIgnatz Mod
    Posts: 5,396

    Yes, create a table of points all the way round the outside, then use triangulate to make them into triangles that you can use for a mesh

  • Thanks, are there any tutorials on how to do this?

  • dave1707dave1707 Mod
    Posts: 8,056

    @thebombdiggity I'm not sure if you can use this, but it might give you some ideas. Tap the screen to create a triangle, then tap to add more points/shapes.

    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
    
  • edited March 2015 Posts: 192
    --use varargs to allow for any number of points
    --takes a number of vec2's and turns them into a polygon.
    function poly(...)
    --set up mesn
    local m=mesh()
    --set up table of arguments
    local arg={...}
    --only draw if there are more than two arguments(vec2's)
    if #arg>2 then
    m.vertices=triangulate(arg)
    m.setColors(fill()) --Edit: does work. Basically use poly like a 'normal' shape and put fill() before it
    --draw the mesh
    m:draw()
    --notify the user of the lack of vertices
    else print("Not enough vertices")
    end
    end
    

    edit: documented the code. Sorry about that-just copied and pasted from a project and forgot to document.

  • @dave1707 thank you so much! This is a great example!

  • @TheSolderKing how exactly would I implement this piece of code? It looks very intriguing but I can't figure out how to run it.

  • Posts: 1,976

    @TheSolderKing Doesn't that code have a massive memory leak with creating all those meshes?

  • Posts: 2,042

    @SkyTheCoder, the best way is to set that function to return a mesh, then just draw that mesh later. That way it only creates it once

  • edited March 2015 Posts: 2,020
    function setup()
        fill(0, 97, 255, 255)
        poly=polygon (vec2(50,50), vec2(100,50), vec2(200,100), vec2(50, 200))
    end
    
    function polygon(...)
        local m=mesh()
        local arg={...}
        m.vertices=triangulate(arg)
        m:setColors(color(fill()))
        return m
    end
    
    function draw()
        background(27, 27, 54, 255)
        poly:draw()
    end
    

    Question: this works on Lua 5.2, but will this break under Lua 5.3? Haven't quite digested the new rules for variable arguments.... Edit: @thebombdiggity the above should work and be future proof

  • Posts: 2,020

    I just need to add local arg={...}?

  • Jmv38Jmv38 Mod
    Posts: 3,295

    yes

  • Thank you @yojimbo2000 for clarifying that. As a follow up question to my original post if I do something like this:

    function setup()
        fill(0, 97, 255, 255)
        pos= vec2(200,0)
        poly=polygon (vec2(pos.y,pos.x), vec2(pos.x,pos.y), vec2(pos.y,pos.y))
    end
    
    function polygon(...)
        local m=mesh()
        local arg={...}
        m.vertices=triangulate(arg)
        m:setColors(color(fill()))
        return m
    end
    
    function draw()
        background(27, 27, 54, 255)
        translate(pos.x,pos.y)
        tween(3.0,pos,{x = 0,y=500})
        print(pos.y)
        poly:draw()
    end
    

    It gets gets really weird toward the end of the animation. Is there a way to make the mesh move from one point to another without it doing that? I have tried different easing types which have helped but don't solve the problem

  • dave1707dave1707 Mod
    Posts: 8,056

    @thebombdiggity You shouldn't call tween in draw because it's doing it 60 times a second. You only need to create it once and wait for it to finish if you need to call it again. Try this example.

    function setup()
        fill(0, 97, 255, 255)
        pos= vec2(200,0)
        poly=polygon (vec2(pos.y,pos.x), vec2(pos.x,pos.y), vec2(pos.y,pos.y))
        createTween()
    end
    
    function polygon(...)
        local m=mesh()
        local arg={...}
        m.vertices=triangulate(arg)
        m:setColors(color(fill()))
        return m
    end
    
    function draw()
        background(27, 27, 54, 255)
        translate(pos.x,pos.y)
        poly:draw()
    end
    
    function createTween()
        pos={x=200,y=0}
        tween(3,pos,{x=0,y=500},tween.easing.linear)
    end
    
    
  • edited March 2015 Posts: 547

    @thebombdiggity, you're starting a tween every frame in the draw function, you shouldn't do that, it keeps starting a new tween, and getting confused with coordinates etc, try calling the tween in setup (after declaring the poly)

    edit: darn it, didn't notice that @dave1707 got it before me xd

  • I can't belive I didn't think about that. Thanks for the help @dave1707 and @stevon8ter

  • edited March 2015 Posts: 192

    Sorry about the memory leak, didn't notice that. Anyways, the way @dave1707 said works better. I can't believe I didn't notice that, sorry again. What are you thinking of using the polygon for? How you use the tween function depends on what you want to do, and I would be glad help you with that even though I'm awful at Tweens :D

  • Posts: 289

    how to text a letter's outline on screen with the above mesh code

  • Like this? You don't even need the above function.

    pos=vec2(WIDTH/2,HEIGHT/2)
        a = mesh()
        vertsA={
        vec2(pos.x-5,pos.y+20),
        vec2(pos.x+5,pos.y+20),
        vec2(pos.x+10,pos.y-10),
        vec2(pos.x+7,pos.y-10),
        vec2(pos.x+2,pos.y),
        vec2(pos.x-2,pos.y),
        vec2(pos.x-7,pos.y-10),
        vec2(pos.x-10,pos.y-10)
        }
        a.vertices=triangulate(vertsA)
        a:setColors(255,255,255)
        b = mesh()
        vertsB={
        vec2(pos.x-10,pos.y-10),
        vec2(pos.x-10,pos.y+10),
        vec2(pos.x,pos.y+10),
        vec2(pos.x+6,pos.y+6),
        vec2(pos.x+2,pos.y),
        vec2(pos.x+6,pos.y-6),
        vec2(pos.x,pos.y-10)
        }
        b.vertices=triangulate(vertsB)
        b:setColors(255,255,255)
    

    I just did a+b, but you can do more and I might do a class that allows you to do this, if there is enough of a need.

  • Posts: 289

    i mean text a letter's outline in any font any fontsize,there might be an algorithm or trick.

  • Posts: 1,976

    @firefox What about a shader that gives text an outline? You'd need to render the text into an image and use a mesh, but it would give the best results.

  • Posts: 1,595

    @SkyTheCoder I've made a shader that does this, if you search Text outline shader it should come up, works exactly how you said

  • IgnatzIgnatz Mod
    Posts: 5,396

    It's easier without shaders, if you don't understand them

  • Posts: 289

    thanks,i wanna change y value of text()when touching in @Luatee's code,how to make it?

  • Posts: 289

    oh,i know how to do,thanks all.

Sign In or Register to comment.