A good, reliable and performant Voronoi implementation
Posted: Wed Jul 16, 2014 10:59 am
Hi everyone.
I have been looking for quite a while to find a good Lua implementation of the Voronoi diagram. (For those who don’t know - look it up on Wikipedia - it’s very useful for creating maps.)
The libraries I was able to find have proven unsatisfactory: iVoronoi is buggy and others were doing the Delaunay triangulation and I would have to compute the Voronoi diagram from that.
I finally found a great thread on the topic which presented a Voronoi implementation for the Corona SDK:
http://forums.coronalabs.com/topic/1947 ... eneration/
I decided to reimplement it so it can be used with Love (or with anything for that matter).
You use it like this:
and then:
(I.e., you feed the function a list of point coordinates and it returns a list of vertices and segments of the Voronoi diagram.)
vertex, segments and list_of_points are lists of points in the form {x=x; y=y}. Points in the list_of_points have additional fields “dx” and “dy” so you can move them around but you can easily edit this away for simplicity.
The attached love file implements the demo found in the original thread where the points keep moving and are recalculated on the fly - which shows how well-performing this algorithm is. BTW:
There have been two related threads on this forum:
http://www.love2d.org/forums/viewtopic. ... noi#p74268 - gives a nice demo of a Voronoi diagram but it uses a simplistic method to derive the diagram and the performance plummets for as low amounts of points as a few hundred. The implementation I have used remains usable for even 100,000 points on my machine.
The other thread:
http://www.love2d.org/forums/viewtopic. ... noi#p40235 - I only found it now and it is based on the same algorithm I used but I couldn’t get it to work in the current form, and it also uses Love-specific drawing commands in the voronoi file directly - with my version, you can do the calculations separately from displaying them.
I hope you find this useful.
Edit. Adding a screenshot:
I have been looking for quite a while to find a good Lua implementation of the Voronoi diagram. (For those who don’t know - look it up on Wikipedia - it’s very useful for creating maps.)
The libraries I was able to find have proven unsatisfactory: iVoronoi is buggy and others were doing the Delaunay triangulation and I would have to compute the Voronoi diagram from that.
I finally found a great thread on the topic which presented a Voronoi implementation for the Corona SDK:
http://forums.coronalabs.com/topic/1947 ... eneration/
I decided to reimplement it so it can be used with Love (or with anything for that matter).
You use it like this:
Code: Select all
voronoi = require”voronoi”
Code: Select all
vertex, segments = voronoi(list_of_points)
vertex, segments and list_of_points are lists of points in the form {x=x; y=y}. Points in the list_of_points have additional fields “dx” and “dy” so you can move them around but you can easily edit this away for simplicity.
The attached love file implements the demo found in the original thread where the points keep moving and are recalculated on the fly - which shows how well-performing this algorithm is. BTW:
There have been two related threads on this forum:
http://www.love2d.org/forums/viewtopic. ... noi#p74268 - gives a nice demo of a Voronoi diagram but it uses a simplistic method to derive the diagram and the performance plummets for as low amounts of points as a few hundred. The implementation I have used remains usable for even 100,000 points on my machine.
The other thread:
http://www.love2d.org/forums/viewtopic. ... noi#p40235 - I only found it now and it is based on the same algorithm I used but I couldn’t get it to work in the current form, and it also uses Love-specific drawing commands in the voronoi file directly - with my version, you can do the calculations separately from displaying them.
I hope you find this useful.
Edit. Adding a screenshot: