Last active
January 8, 2022 10:41
-
-
Save cormullion/705e97f4c7b7e4dc994b15ddfdb28cd4 to your computer and use it in GitHub Desktop.
Delaunay to Voronoi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An example made with this code: