Mandelbrot set: Difference between revisions

→‎{{header|Ruby}}: add raster version from Tcl
(add Ruby)
(→‎{{header|Ruby}}: add raster version from Tcl)
Line 412:
 
=={{header|Ruby}}==
{{needs-review|Ruby|This example does not use raster graphics operations.}}
Text only, prints an 80-char by 41-line depiction. Found [http://www.xcombinator.com/2008/02/22/ruby-inject-and-the-mandelbrot-set/ here].
<lang ruby>require 'complex'
Line 426 ⟶ 425:
puts
end</lang>
 
{{trans|Tcl}}
 
Uses the text progress bar from [[Median filter#Ruby]]
<lang ruby>class RGBColour
def self.mandel_colour(i)
self.new( 16*(i % 15), 32*(i % 7), 8*(i % 31) )
end
end
 
class Pixmap
def self.mandelbrot(width, height)
mandel = Pixmap.new(width,height)
pb = ProgressBar.new(width) if $DEBUG
width.times do |x|
height.times do |y|
x_ish = Float(x - width*11/15) / (width/3)
y_ish = Float(y - height/2) / (height*3/10)
mandel[x,y] = RGBColour.mandel_colour(mandel_iters(x_ish, y_ish))
end
pb.update(x) if $DEBUG
end
pb.close if $DEBUG
mandel
end
 
def self.mandel_iters(cx,cy)
x = y = 0.0
count = 0
while Math.hypot(x,y) < 2 and count < 255
x, y = (x**2 - y**2 + cx), (2*x*y + cy)
count += 1
end
count
end
end
 
Pixmap.mandelbrot(300,300).save('mandel.ppm')</lang>
 
=={{header|Tcl}}==
Anonymous user