Bitmap/Flood fill: Difference between revisions

no edit summary
(→‎{{header|Processing}}: adding Processing Python mode)
No edit summary
Line 2,510:
bitmap.draw_circle(Pixel[200,100], 40, RGBColour::BLACK)
bitmap.flood_fill(Pixel[140,160], RGBColour::BLUE)</lang>
 
{{libheader|RubyGems}}
{{libheader|JRubyArt}}
JRubyArt is a port of Processing to the ruby language
 
<lang ruby>
# holder for pixel coords
Pixel = Struct.new(:x, :y)
 
attr_reader :img, :fill_color, :queue, :value
 
def setup
sketch_title 'Flood Fill'
@img = load_image(data_path('image.png'))
@fill_color = color(250, 0, 0)
fill(0, 0, 100)
image(img, 0, 0, width, height)
end
 
def draw
image(img, 0, 0, width, height)
no_loop
end
 
def mouse_clicked
img.load_pixels
flood(mouse_x, mouse_y)
img.update_pixels
redraw
end
 
def flood(x, y)
@queue = Queue.new
queue.enq(Pixel.new(x, y))
until queue.empty?
pix = queue.pop
next unless check(pix, color(255))
 
queue.enq(Pixel.new(pix.x, pix.y - 1))
queue.enq(Pixel.new(pix.x, pix.y + 1))
queue.enq(Pixel.new(pix.x - 1, pix.y))
queue.enq(Pixel.new(pix.x + 1, pix.y))
end
end
 
def check(pix, target_color)
unless (1...width).include?(pix.x) && (1...height).include?(pix.y)
return false
end
 
value = img.pixels[pix.x + (pix.y * img.width)]
return false if target_color != value
 
img.pixels[pix.x + (pix.y * img.width)] = fill_color
true
end
 
def settings
size(256, 256)
end
</ruby>
 
=={{header|Rust}}==
Anonymous user