H-tree fractal

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 require 'builder' def hTree ( xml , level , x , y , length , starting_length ) stroke_width = 8 . 0 * ( length / starting_length ) if level > 0 if level % 2 == 0 xml . line :x1 => x - length / 1 . 0 , :y1 => y , :x2 => x + ( length / 1 . 0 ), :y2 => y , :style => "stroke:rgb(255,0,0);stroke-width: #{ stroke_width } " hTree ( xml , level - 1 , x - length , y , length , starting_length ) hTree ( xml , level - 1 , x + length , y , length , starting_length ) else xml . line :x1 => x , :y1 => y - length / 1 . 0 , :x2 => x , :y2 => y + ( length / 1 . 0 ), :style => "stroke:rgb(255,0,0);stroke-width: #{ stroke_width } " hTree ( xml , level - 1 , x , y - length , length / 2 . 0 , starting_length ) hTree ( xml , level - 1 , x , y + length , length / 2 . 0 , starting_length ) end end end xml = Builder : :XmlMarkup . new ( :target => STDOUT , :ident => 2 ) level = 10 length = 175 xml . svg :version => "1.1" , "xmlns" => "http://www.w3.org/2000/svg" , "xmlns:xlink" => "http://www.w3.org/1999/xlink" do hTree ( xml , level , 600 , 400 , length , length ) end

Download

This Ruby code creates an H-tree fractal as a SVG vector image, each level is created recursively. Every other level the length havles. For odd levels the X axis is drawen, for even levels the Y axis is drawen.