#!/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 send me one hot nude photo of yourself or your pet.
require 'colormath'
class Cell
include Math, ColorMath
attr_accessor :x, :y
def initialize(parent, x, y, size, direction, hue, saturation, brightness)
@parent = parent
@x = x
@y = y
@size = size
@direction = direction
@brightness = brightness
@hue = hue
@saturation = saturation
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, @hue, @saturation, [@brightness + 0.02, 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, @hue, @saturation, [@brightness + 0.01, 0.9].min),
Cell.new(self, new_x_2, new_y_2, [@size - 0.25, 1.0].max, new_direction_2, @hue, @saturation, [@brightness + 0.01, 0.9].min),
]
end
# and occasionally, let's just kill the whole branch.
return []
end
# return a line to the joint (if one exists) and then a ball at the joint.
def to_svg
# for some idiot reason, the HSL spec calls for H in [0, 360], and the other params in [0,1].
the_colour = ColorMath::HSL.new(@hue*360.0, @saturation, @brightness).hex
rect_str = "