Page 1 of 1
draft - a powerful drawing library
Posted: Mon Aug 17, 2015 7:03 am
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!
Re: draft - a powerful drawing library
Posted: Mon Aug 17, 2015 9:44 am
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!
Re: draft - a powerful drawing library
Posted: Mon Aug 17, 2015 5:25 pm
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!
Re: draft - a powerful drawing library
Posted: Mon Aug 17, 2015 6:07 pm
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
Re: draft - a powerful drawing library
Posted: Tue Aug 18, 2015 7:42 am
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
Re: draft - a powerful drawing library
Posted: Tue Aug 18, 2015 1:14 pm
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.
Re: draft - a powerful drawing library
Posted: Tue Aug 18, 2015 10:53 pm
by AntonioModer
I need a screenshot, please!
Re: draft - a powerful drawing library
Posted: Wed Aug 19, 2015 1:27 am
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 (69.76 KiB) Viewed 5539 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 (311.26 KiB) Viewed 5539 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!
Re: draft - a powerful drawing library
Posted: Wed Aug 19, 2015 2:03 am
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
Re: draft - a powerful drawing library
Posted: Thu Aug 20, 2015 3:24 am
by AntonioModer
Thank you
pel.