Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active May 22, 2016 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/ed7c2c52810440d1f5d732aea78c634e to your computer and use it in GitHub Desktop.
Save JoshCheek/ed7c2c52810440d1f5d732aea78c634e to your computer and use it in GitHub Desktop.
Chaos in Fibonacci sequence bit representation
require 'chunky_png'
def bit_triangle(ints)
# colors taken from Espresso Libre theme for TextMate
brown = ChunkyPNG::Color.rgb(0x38, 0x2c, 0x25)
blue = ChunkyPNG::Color.rgb(0x00, 0x66, 0xff)
green = ChunkyPNG::Color.rgb(0x43, 0xaa, 0x43)
bg_color = brown
bit_color = green
width = ints.length
height = ints.max.bit_length
png = ChunkyPNG::Image.new width, height, bg_color
ints.each_with_index do |int, x|
int.bit_length.times do |bit_index|
y = height - bit_index - 1 # 0,0 is topleft, this inverts it to botleft
png[x, y] = bit_color if int[bit_index].zero?
end
end
png
end
# Fibonacci numbers
#
# Got the idea from
# http://mathworld.wolfram.com/FibonacciNumber.html
# Definition of the golden ratio from Euclid's Elements:
# A straight line is said to have been cut in extreme and mean ratio when,
# as the whole line is to the greater segment, so is the greater to the less.
# http://aleph0.clarku.edu/~djoyce/java/elements/bookVI/defVI3.html
fibs = [0, 1]
fib = -> n { fibs[n] ||= fib[n-1] + fib[n-2] }
png = bit_triangle (0..590).map(&fib)
file = 'fib.png'
png.save(file)
exec 'open', file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment