Skip to content

Instantly share code, notes, and snippets.

@xoolive
Created September 30, 2014 21:20
Show Gist options
  • Save xoolive/9e766f2c937ae8cc872d to your computer and use it in GitHub Desktop.
Save xoolive/9e766f2c937ae8cc872d to your computer and use it in GitHub Desktop.
Mandelbrot fractal in console
#! /bin/ruby
#
require 'complex'
class Mandelbrot
def initialize(bailout=10, iterations=100)
@bailout, @iterations = bailout, iterations
end
def mandelbrot(x, y)
c = Complex(x, y)
z = 0
@iterations.times do |i|
z = z**2 + c
return false if z.abs > @bailout
end
return true
end
def render(x_size=76, y_size=30, inside_set="*", outside_set=" ")
0.upto(y_size) do |y|
0.upto(x_size) do |x|
scaled_x = -2 + (3 * x / x_size.to_f)
scaled_y = -2 + (3 * y / y_size.to_f)
print mandelbrot(scaled_x, scaled_y) ? inside_set : outside_set
end
puts
end
end
end
Mandelbrot.new.render()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment