Voronoi diagram: Difference between revisions

m
→‎{{header|Sidef}}: added link to output file + code simplifications
m (→‎{{header|Perl 6}}: missed a bar)
m (→‎{{header|Sidef}}: added link to output file + code simplifications)
Line 2,442:
 
func generate_voronoi_diagram(width, height, num_cells) {
var img = %sO<Imager>.new(xsize => width, ysize => height)
var (nx,ny,nr,ng,nb) = 5.of { [] }...
 
for i in (^num_cells) {
nx << rand(^width)
ny << rand(^height)
Line 2,453:
}
 
for y in =(^height), x=(^width) {
var j = (^num_cells -> min_by var d ={|i| hypot(nx[i]-x, ny[i]-y) })
for x in ^width {
img.setpixel(x => x, y var=> dminy, color => hypot(width-1[nr[j], ng[j], height-1nb[j]])
var j = -1
for i in ^num_cells {
var d = hypot(nx[i]-x, ny[i]-y)
if (d < dmin) { (dmin, j) = (d, i) }
}
img.setpixel(x => x, y => y, color => [nr[j], ng[j], nb[j]])
}
}
return img
Line 2,469 ⟶ 2,462:
var img = generate_voronoi_diagram(500, 500, 25)
img.write(file => 'VoronoiDiagram.png')</lang>
Output image: [https://github.com/trizen/rc/blob/master/img/voronoi-diagram-sidef.png Voronoi diagram]
 
=={{header|Tcl}}==
2,747

edits