Bitmap/Flood fill: Difference between revisions
Content added Content deleted
MaiconSoft (talk | contribs) (Added Delphi reference to Pascal code) |
|||
Line 2,593: | Line 2,593: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Uses [[Raster graphics operations/Ruby]] |
|||
'''Note''' This code is not completely functional. Please add the remaining classes (Pixel, ..) and initializers. Or maybe a library to be included to make this work. |
|||
<lang ruby> |
<lang ruby># frozen_string_literal: true |
||
require_relative 'raster_graphics' |
|||
class RGBColour |
|||
def ==(a_colour) |
def ==(a_colour) |
||
values == a_colour.values |
values == a_colour.values |
||
end |
end |
||
end |
|||
class Queue < Array |
|||
alias_method :enqueue, :push |
|||
alias_method :dequeue, :shift |
|||
end |
end |
||
Line 2,613: | Line 2,612: | ||
until queue.empty? |
until queue.empty? |
||
p = queue.dequeue |
p = queue.dequeue |
||
next unless self[p.x, p.y] == current_colour |
|||
west = find_border(p, current_colour, :west) |
|||
west = find_border(p, current_colour, :west) |
|||
east = find_border(p, current_colour, :east) |
|||
draw_line(west, east, new_colour) |
|||
q = west |
|||
while q.x <= east.x |
|||
[:north, :south].each do |direction| |
|||
%i[north south].each do |direction| |
|||
n = neighbour(q, direction) |
|||
queue.enqueue(n) if self[n.x, n.y] == current_colour |
|||
end |
|||
⚫ | |||
end |
end |
||
⚫ | |||
end |
end |
||
end |
end |
||
Line 2,649: | Line 2,648: | ||
bitmap = Pixmap.new(300, 300) |
bitmap = Pixmap.new(300, 300) |
||
bitmap.draw_circle(Pixel[149,149], 120, RGBColour::BLACK) |
bitmap.draw_circle(Pixel[149, 149], 120, RGBColour::BLACK) |
||
bitmap.draw_circle(Pixel[200,100], 40, RGBColour::BLACK) |
bitmap.draw_circle(Pixel[200, 100], 40, RGBColour::BLACK) |
||
bitmap.flood_fill(Pixel[140,160], RGBColour::BLUE) |
bitmap.flood_fill(Pixel[140, 160], RGBColour::BLUE) |
||
bitmap.save_as_png('flood_fill.png')</lang> |
|||
{{libheader|RubyGems}} |
{{libheader|RubyGems}} |