Anonymous user
Voronoi diagram: Difference between revisions
m
→{{header|Ruby}}
Line 2,474:
Uses [[Raster graphics operations/Ruby]]
[[File:voronoi_rb.png|thumb|right|Sample output from Ruby program]]
<lang ruby>
require_relative 'raster_graphics'
class ColourPixel < Pixel
Line 2,484 ⟶ 2,486:
def distance_to(px, py)
Math
end
end
width
height = 200
npoints = 20
pixmap = Pixmap.new(width, height)
@bases = npoints.times.collect do |
ColourPixel.new(
)
end
pixmap.each_pixel do |x, y|
nearest = @bases.min_by { |base| base.distance_to(x, y) }
pixmap[x, y] = nearest.colour
end
@bases.each do |base|
pixmap[base.x, base.y] = RGBColour::BLACK
pixmap.draw_circle(base, 2, RGBColour::BLACK)
end
pixmap.save_as_png(
{{libheader|RubyGems}}
|