Skip to content

Instantly share code, notes, and snippets.

@cormullion
Last active January 8, 2022 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cormullion/705e97f4c7b7e4dc994b15ddfdb28cd4 to your computer and use it in GitHub Desktop.
Save cormullion/705e97f4c7b7e4dc994b15ddfdb28cd4 to your computer and use it in GitHub Desktop.
Delaunay to Voronoi
# should run this using Luxor#master or >= v2.20.0 -
# I replaced a dodgy implementation of Jarvis March with
# a hopefully less dodgy implementation of Graham Scan.
using Luxor, ColorSchemes
@draw begin
background("black")
println("create points")
verts = randompointarray(BoundingBox(), 30)
println("triangulate")
triangles = polytriangulate(verts)
println("draw the triangulation")
@layer begin
for tri in triangles
randomhue()
poly(tri, :stroke, close=true)
end
end
dict = Dict{Point, Vector{Int}}()
println("build point -> triangle dictionary")
for (n, t) in enumerate(triangles)
for pt in t
if haskey(dict, pt)
push!(dict[pt], n)
else
dict[pt] = [n]
end
end
end
println("build cells")
setopacity(0.9)
setline(3)
for v in verts
hull = Point[]
# vertex v belongs in triangle tri
tris = dict[v]
print(" Δ")
for tri in tris
push!(hull, trianglecenter(triangles[tri]...))
end
sethue(get(ColorSchemes.tableau_colorblind, rand()))
print("⛵")
ph = polyhull(hull)
print("✏")
poly(ph, :fillpreserve, close=true)
sethue("black")
strokepath()
end
end 500 500
@cormullion
Copy link
Author

An example made with this code:

Screenshot 2022-01-06 at 11 56 23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment