Skip to content

Instantly share code, notes, and snippets.

@shakesoda
Created November 29, 2011 23:57
Show Gist options
  • Save shakesoda/1407242 to your computer and use it in GitHub Desktop.
Save shakesoda/1407242 to your computer and use it in GitHub Desktop.
require 'rmagick'
#
# simple mandelbrot set using imagemagick
#
# settings
maxIterations = 50
factor = 256.0
include Magick
image = Image.new(512, 512) do
self.background_color = 'white'
end
# create canvas and fill colors
gradient = GradientFill.new(0, 0, 100, 0, '#00f', '#f00')
fill = Image.new(maxIterations, 1, gradient)
insideColor = Pixel.from_color('black')
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/277157
def dot_product l1, l2
sum = 0
for i in 0...l1.size
sum += l1[i] * l2[i]
end
sum
end
c = []
# iterate through each pixel
image.each_pixel do |pixel, x, y|
c[0] = (x / factor) - 1.0
c[1] = (y / factor) - 1.0
z = c
image.pixel_color(x, y, insideColor)
maxIterations.times do |i|
z[0] = (c[0] * c[0] - c[1] * c[1]) + c[0]
z[1] = (c[0] * c[1] * 2.0) + c[1]
if dot_product(z, z) > 5.0
image.pixel_color(x, y, fill.pixel_color(i, 1))
break
end
end
end
image.display
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment