# Re: Fractals (#125)

**URL:** <https://www.ruby-forum.com/t/re-fractals-125/105559>\
**Category:** Ruby\
**Created:** [May 31, 2007, 4:15am UTC](https://www.ruby-forum.com/t/re-fractals-125/105559 "2007-05-31T04:15:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [May 31, 2007, 4:15am UTC](https://www.ruby-forum.com/t/re-fractals-125/105559/1 "2007-05-31T04:15:04Z")

</div>

#---------------------------------------------------------------------#

# 

# Program : Fractals (Ruby Q. #125)

# Author : David T.

# Date : 2007-05-30

# Blog : [http://davidtran.doublegifts.com/blog/?p=48](http://davidtran.doublegifts.com/blog/?p=48)

# Reference : [Perpendicular Vector -- from Wolfram MathWorld](http://mathworld.wolfram.com/PerpendicularVector.html)

# Note : Using vector calculation to compute each level’s

# points. The first level line can be in any direction.

# 

#---------------------------------------------------------------------#  
require ‘enumerator’  
require ‘RMagick’

LEVEL\_0 = [[0, 0], [350, 200]]

def next\_level(polylines)  
polylines.enum\_cons(2).inject([polylines.first]) do |array, (p1, p2)|  
x1, y1 = p1  
x2, y2 = p2  
a = (x2 - x1) / 3.0  
b = (y2 - y1) / 3.0  
array \<\< [x1+a, y1+b] \<\< [x1+a-b, y1+b+a] \<\<  
[x1+2_a-b, y1+2_b+a] \<\< [x1+2_a, y1+2_b] \<\< p2  
end  
end

exit unless **FILE** == $0  
imageList = Magick::ImageList.new  
level = LEVEL\_0  
(ARGV[0].to\_i + 1).times do |i|  
level = next\_level(level) if i \> 0  
image = Magick::Image.new(400, 300)  
image.delay = 100  
draw = Magick::Draw.new  
draw.fill\_opacity(0)  
draw.stroke(‘black’)  
draw.polyline(\*level)  
draw.text(300,100,“level #{i}”)  
draw.draw(image)  
imageList \<\< image  
end  
imageList.write(File.basename($0) + “.gif”)
