draft - a powerful drawing library

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
pel
Prole
Posts: 28
Joined: Mon Aug 17, 2015 6:48 am

draft - a powerful drawing library

Post by pel »

Hi guys,

I just updated my draft library to run with Love2D version 0.9.2.

I thought I would share it with the community!

The library extends the graphic primitives offered by Love2D (line, rectangle, polygon, etc..).

It makes it easy to draw such shapes as a trapezoid, a rhombus, a gem, a diamond, a kite, a bow, a dome, and many more...

One of the most powerful functions is the compass. draft:compass. It can be used to draw all kinds of curved shapes. You can even draw luxurious shapes with it, i.e. draft:star, draft:egg. Check it out!

Also, there are four functions called linkers which are quite powerful. They link a shape's vertice points to each other. For example, you could draw two independent shapes then have all their points interconnect with a linker.

The code lives on github: https://github.com/pelevesque/draft

It is completely free. If you use it, I just ask that you star the github repo. Thank you!
Last edited by pel on Wed Aug 19, 2015 2:07 am, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: draft - a powerful drawing library

Post by zorg »

One thing,
github wrote:Due to LÖVE 2D drawing limitations, the draft:star function fails in fill mode. This is because LÖVE 2D is unable to properly fill concave polygons.
You can make use of [wiki]love.math.triangulate[/wiki] to decompose convex polygons into triangles, then draw those filled; a few more draw calls, but it works!
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
pel
Prole
Posts: 28
Joined: Mon Aug 17, 2015 6:48 am

Re: draft - a powerful drawing library

Post by pel »

Thanks, I'll try that. Is it a new function in 0.9.2? I wrote this library a while back, like 2012, and just started updating it recently for the new version. Eager to try your triangulate idea. Gonna see if I can find some time today!
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: draft - a powerful drawing library

Post by zorg »

It's actually a 0.9.0 function (also, the wiki does tell you when a function was introduced / deprecated)
glad to have helped :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
pel
Prole
Posts: 28
Joined: Mon Aug 17, 2015 6:48 am

Re: draft - a powerful drawing library

Post by pel »

Thank you again!

I fixed the drawing of concave polygons with the love.math.isConvex() and love.math.triangulate() methods.

As you said, rather easy.

Now I'm wondering why Love 2D doesn't simply do this inside the native polygon method?

Before

Code: Select all

--[[
  Draws a polygon
  @param  table   vertices {x1, y1, x2, y2, ...}
  @param  mixed  drawMode (fill, line, false) [def: self.mode]
  @return  table   vertices {x1, y1, x2, y2, ...}
--]]
function draft:polygon(vertices, mode)
    if mode == nil then mode = self.mode end
    if mode then
        love.graphics.polygon(mode, vertices)
    end
    return vertices
end
After

Code: Select all

--[[
  Draws a polygon
  @param  table   vertices {x1, y1, x2, y2, ...}
  @param  mixed  drawMode (fill, line, false) [def: self.mode]
  @return  table   vertices {x1, y1, x2, y2, ...}
--]]
function draft:polygon(vertices, mode)
    if mode == nil then mode = self.mode end
    if mode then
        if (mode == 'fill' and not love.math.isConvex(vertices)) then
            local triangles = love.math.triangulate(vertices)
            for _, v in pairs(triangles) do
               love.graphics.polygon('fill', v)
            end
        else
            love.graphics.polygon(mode, vertices)
        end
   end
   return vertices
end
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: draft - a powerful drawing library

Post by davisdude »

pel wrote:Now I'm wondering why Love 2D doesn't simply do this inside the native polygon method?
I've wondered the same thing too. See here.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
pel
Prole
Posts: 28
Joined: Mon Aug 17, 2015 6:48 am

Re: draft - a powerful drawing library

Post by pel »

Right, of course, screenshots!

BTW - I updated my repo. While making these two screenshots, I realized there was a bug in my triangle drawing routines. So, thank you! Should be bug free now.

Here is a screen shot of the test.lua file running. It runs all the methods in draft, so it gives a good overview of possible shapes.
test.lua.jpg
test.lua.jpg (69.76 KiB) Viewed 5376 times
Here is a screenshot of the example_linker.lua file running. You should run it yourself since it evolves over time!

Basically, I draw the egg shape so large that outside of the egg is outside the screen. Then I use draft:linkWeb to link all the points of the polygon to each other creating a tangled web of lines. You can do some pretty crazy stuff with the linkers. You should really try it.
example_linker.lua.jpg
example_linker.lua.jpg (311.26 KiB) Viewed 5376 times

Seriously, the draft:compass method is really powerful for drawing arcs and other curved shapes. And, as I stated above, the linkers are also very powerful.

I suggest you read the code. It's pretty clear.

Try it out!
pel
Prole
Posts: 28
Joined: Mon Aug 17, 2015 6:48 am

Re: draft - a powerful drawing library

Post by pel »

A quick extra note on the draft:compass method.

It's powerful because the scale argument accepts a function. This means you can change the size of the compass's arm as it circles around doing its drawing. Essentially, this means you are changing the size of the radius. That's how I draw the egg and the star shapes.

It's like the Spirograph game for kids.

You can do all sorts of drawings with it.

The library is worth it for this method alone.

It's based on this code: http://slabode.exofire.net/circle_draw.shtml
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest