JCash/defold-voronoi/voronoi
Creates bounded two-dimensional Voronoi diagrams using jc_voronoi 0.10.0.
- GitHub: JCash/defold-voronoi — pinned to
v0.10.0(released while Defold 1.13.0 was current)- Pick a release from JCash/defold-voronoi releases and add its Source code (zip) URL (or a packaged
.zipasset, if the library ships one) togame.projectunder[project]dependencies, then Fetch Libraries in the Defold editor. - Run
bunx @defold-typescript/cli resolveto materialize its types. - Import it under a namespace alias of your choice:
import * as voronoi from "voronoi"
- Pick a release from JCash/defold-voronoi releases and add its Source code (zip) URL (or a packaged
- Asset Portal: defold-voronoi
Functions
voronoi.create(width: number, height: number): userdatavoronoi.destroy(diagram: userdata)voronoi.generate(diagram: userdata, points: table)voronoi.relax(diagram: userdata)voronoi.get_sites(diagram: userdata): tablevoronoi.get_num_sites(diagram: userdata): numbervoronoi.get_site(diagram: userdata, index: number): tablevoronoi.get_num_vertices(diagram: userdata): numbervoronoi.get_vertices(diagram: userdata): tablevoronoi.get_edges(diagram: userdata): tablevoronoi.get_delauney_edges(diagram: userdata): tablevoronoi.get_debug_image(diagram: userdata): userdata
voronoi.create(width: number, height: number): userdata
Creates a reusable Voronoi diagram context for a rectangular area.
local diagram = voronoi.create(800, 600)Parameters
width:number— Width of the diagram bounds in coordinate units.height:number— Height of the diagram bounds in coordinate units.
Returns
diagram:userdata— Diagram context passed to the other voronoi functions.
voronoi.destroy(diagram: userdata)
Releases a diagram context. Do not use the context after destroying it.
voronoi.destroy(diagram)Parameters
diagram:userdata— Diagram context returned by voronoi.create().
voronoi.generate(diagram: userdata, points: table)
Replaces the diagram with sites generated from a flat list of x/y coordinates. Duplicate sites may be removed.
voronoi.generate(diagram, { 100, 100, 400, 300, 700, 500 })Parameters
diagram:userdata— Diagram context returned by voronoi.create().points:table— Flat coordinate array in the form { x1, y1, x2, y2, ... }.
voronoi.relax(diagram: userdata)
Moves every input point toward the average of its site and cell vertices, then regenerates the diagram.
voronoi.relax(diagram)Parameters
diagram:userdata— Generated diagram context.
voronoi.get_sites(diagram: userdata): table
Returns every generated site. A site contains p, index, boundary, edges, and the convenience field area. The p field is { x, y }; index is the 1-based original input index; boundary reports whether the cell touches the clipping boundary; and edges contains counter-clockwise edge tables.
for _, site in ipairs(voronoi.get_sites(diagram)) do pprint(site) endParameters
diagram:userdata— Generated diagram context.
Returns
sites:table— Array of site tables. ⚠️ 1-based; passed tovoronoiunchanged.
voronoi.get_num_sites(diagram: userdata): number
Returns diagram.numsites, the number of unique generated sites. This may be smaller than the input count when points are duplicated.
Parameters
diagram:userdata— Generated diagram context.
Returns
count:number— Number of unique generated sites.
voronoi.get_site(diagram: userdata, index: number): table
Returns the generated site for a 1-based original input index. The site has the same fields as entries returned by get_sites().
local site = voronoi.get_site(diagram, 1)Parameters
diagram:userdata— Generated diagram context.index:number— 1-based original input point index. ⚠️ 1-based; passed tovoronoiunchanged.
Returns
site:table— Site table for the requested input point.
voronoi.get_num_vertices(diagram: userdata): number
Returns diagram.numvertices, the number of unique Voronoi vertices.
Parameters
diagram:userdata— Generated diagram context.
Returns
count:number— Number of unique vertices.
voronoi.get_vertices(diagram: userdata): table
Returns all unique Voronoi vertices as { x, y } point tables. Edge vertex indices refer to this array.
Parameters
diagram:userdata— Generated diagram context.
Returns
vertices:table— Array of point tables.
voronoi.get_edges(diagram: userdata): table
Returns every diagram edge. Each edge contains sites, pos, and vertices. sites contains two 1-based input indices, with 0 denoting the clipping boundary. pos contains two { x, y } endpoints. vertices contains two 1-based indices into get_vertices(), with 0 denoting an invalid endpoint.
Parameters
diagram:userdata— Generated diagram context.
Returns
edges:table— Array of edge tables. ⚠️ 1-based; passed tovoronoiunchanged.
voronoi.get_delauney_edges(diagram: userdata): table
Returns the Delauney edges produced by the upstream jcv_delauney iterator. Each entry contains edge, sites, and pos fields matching jcv_delauney_edge. The upstream spelling "delauney" is preserved in the Lua function name.
Parameters
diagram:userdata— Generated diagram context.
Returns
edges:table— Array of Delauney edge tables.
voronoi.get_debug_image(diagram: userdata): userdata
Rasterizes the current diagram into an RGB buffer matching the context dimensions.
resource.set_texture(texture, texture_header, voronoi.get_debug_image(diagram))Parameters
diagram:userdata— Generated diagram context.
Returns
image:userdata— Buffer with a uint8 rgb stream containing three components per pixel.