#!/usr/bin/env ruby
# By T. Kowaliw, 2014, http://kowaliw.ca
# Feel free to use, modify, copy, distribute, sell, abuse, exploit, enslave, ravage, whatevs, provided you mail me one (1) live gerbil.
class Cell
include Math
attr_accessor :x, :y
def initialize(parent, x, y, size, direction, brightness)
@parent = parent
@x = x
@y = y
@size = size
@direction = direction
@brightness = brightness
end
def children
# most of the time, we will just add another child in a direct line from the parent...
if rand < 0.7
new_x = @x + cos(@direction)*@size*5
new_y = @y + sin(@direction)*@size*5
new_direction = @direction
new_direction = new_direction + (rand*0.4 - 0.2) if rand < 0.1
new_direction = new_direction + (rand*1.4 - 0.7) if rand < 0.02
new_size = [@size - 0.25 + rand*0.2, 1.0].max
new_size = [@size +rand - 0.5, 1.0].max if rand < 0.05
return [Cell.new(self, new_x, new_y, new_size, new_direction, [@brightness + 0.03, 0.9].min)]
# sometimes, let's divide
elsif rand < 0.9
new_direction_1 = @direction + 0.3
new_direction_2 = @direction - 0.3
new_x_1 = @x + cos(new_direction_1)*@size*5
new_y_1 = @y + sin(new_direction_1)*@size*5
new_x_2 = @x + cos(new_direction_2)*@size*5
new_y_2 = @y + sin(new_direction_2)*@size*5
return [
Cell.new(self, new_x_1, new_y_1, [@size - 0.25, 1.0].max, new_direction_1, [@brightness + 0.03, 0.9].min),
Cell.new(self, new_x_2, new_y_2, [@size - 0.25, 1.0].max, new_direction_2, [@brightness + 0.03, 0.9].min),
]
# and occasionally, let's just kill the whole branch.
else
return []
end
end
# return a line to the joint (if one exists) and then a ball at the joint.
def to_svg
scaled_colour_val = (@brightness*255).to_i
the_colour = "rgb(#{scaled_colour_val},#{scaled_colour_val},#{scaled_colour_val})"
rect_str = "