Question on drawing and filling non-standard shapes in codea

Hi all,

I’m starting with all this stuff (codea and lua) and got into a question today: is there a way to draw and fill a shape in codea by providing 4 points (like a polygon)?

I would like to generate trapezoid-like shapes and already have the coordinates for all vertices but need to understand if there is already something built-in for that or if I need to figure out my own way of doing that.

Thanks!

For drawing a simple polygon, you could declare all your points in a table, and then loop through the points and draw a line between each point (make sure the points are declared in order)

function draw()
    points = {vec2(0, 0), vec2(0, 50), vec2(50, 0)} -- declare table of points
    for i, point in ipairs(points) do -- loop through all the points
        if i < #points then -- make sure your not on the last point
            line(point.x, point.y, points[i + 1].x, points[i + 1].y) -- Create a line between the current
                                                                                           -- point and the next point
        else -- if it is the last point
            line(point.x, point.y, points[1].x, points[1].y)
        end
    end
end

As for filling, you could try something with physics.body? Im not quite fluent with the physics engine yet (Im working on it), so I wouldn’t be much help there, but you can certainly try to use it

Try this. Let me know if you get stuck.

http://twolivesleft.com/Codea/Talk/discussion/2399/utility%3Afilling-a-vector-object-with-colour

Thanks a lot, i’ll for sure look into this.

Just one more question (that also applies to this thread): what about the performance on doing that? I mean, I would fill & draw about 200 to 300 of those shapes per frame and I’m starting to think this is a bit too much considering this is just the backgound. :slight_smile:

To contextualize: that would be the track from a top-gear/lotus like racing game… Currently I’m using bezels to define the shape of the track (and to deform it in the corners) and need a way to fill it.

If you use my code, the filled shapes will be created from scratch just once, and then stored as images, so unless you keep having to create new ones, you are asking about the performance hit of drawing hundreds of small images.

You should use a mesh, maybe it will replace the need for all your images too, but that is outside my knowledge. I suggest you read up on it and look at projects that use it.

Mesh is the way to go - as long as it’s a “once and done” solution, you can’t beat the mesh. Only exception I can think of is if you ahve a lot of objects stacked or hidden and you call draw() when you don’t need to. That’s an optimization technique that is best left to AFTER your main code is written though :slight_smile: