Voronoi diagram: Difference between revisions

Content added Content deleted
(Add Julia algorithm)
Line 914: Line 914:


=={{header|Julia}}==
=={{header|Julia}}==
First version generates an image with random colors as centroids for the voronoi tesselation:
{{libheader|Images}}
<lang Julia>
<lang julia>
using Images
using Images
function voronoi(w, h, n_centroids)
function voronoi(w, h, n_centroids)
Line 929: Line 929:
end
end
img = voronoi(800, 600, 200)
img = voronoi(800, 600, 200)
</lang>

Second version takes an image as an input, samples random centroids for the voronoi cells, and asignes every pixel within that cell the color of the centroid:

<lang julia>
using TestImages, Images
function voronoi_img!(img, n_centroids)
n,m = size(img)
w = minimum([n,m])
dist = (point,vector) -> sqrt.((point[1].-vector[:,1]).^2 .+ (point[2].-vector[:,2]).^2)
dots = [rand(1:n, n_centroids) rand(1:m, n_centroids)]
c = []
for i in 1:size(dots,1)
p = dots[i,:]
append!(c, [img[p[1],p[2]]])
end
dots = [dots c]
for x in 1:n, y in 1:m
distances = dist([x,y],dots) # distance
nn = findmin(distances)[2]
img[x,y] = dots[nn,:][3]
end
end
img = testimage("mandrill")
voronoi_img!(img, 300)
</lang>
</lang>