Edited at

樹木曲線

More than 1 year has passed since last update.

ちょっとしたフラクタル


描写

require "rmagick"

def draw(i, from, len, rad, r, depth)
to = [
from[0] + len * Math.sin(rad),
from[1] + len * Math.cos(rad)
]

d = Magick::Draw.new
d.stroke("green")
d.line(from[1],from[0],to[1],to[0])
d.draw(i)

#p [from, to, depth]

if depth > 0
draw(i, to, len * 0.7, rad + r, r, depth - 1)
draw(i, to, len * 0.7, rad - r, r, depth - 1)
end
end

il = Magick::ImageList.new
i = il.new_image(500,500)
i.annotate(Magick::Draw.new, 1, 1, 30, 30, n.to_s)
draw(i, [250,250], 50, Math::PI/-2, 0.5, 10)
i.write("tree1.gif")

tree1.gif


さらに描写

il = Magick::ImageList.new

il.delay = 50
for n in 1 .. 40
i = il.new_image(500,500)
i.annotate(Magick::Draw.new, 1, 1, 30, 30, n.to_s)
draw(i, [250,250], 50, Math::PI/-2, Math::PI/10*(0.1*n), 10)
end
i.write("tree2.gif")

tree2.gif


もっと描写

最初の再帰の枝の計算を+から*

def draw(i, from, len, rad, r, depth)

to = [
from[0] + len * Math.sin(rad),
from[1] + len * Math.cos(rad)
]

d = Magick::Draw.new
d.stroke("green")
d.line(from[1],from[0],to[1],to[0])
d.draw(i)

#p [from, to, depth]

if depth > 0
draw(i, to, len * 0.7, rad * r, r, depth - 1)
draw(i, to, len * 0.7, rad - r, r, depth - 1)
end
end

il = Magick::ImageList.new
il.delay = 50
for n in 1 .. 60
i = il.new_image(500,500)
i.annotate(Magick::Draw.new, 1, 1, 30, 30, n.to_s)
draw(i, [250,250], 50, Math::PI/-2, Math::PI/10*(0.1*n), 10)
end
i.write("tree3.gif")

tree3.gif


せわしない描写

一つ目の再帰の枝の計算を+ r

二つ目の再帰の枝の計算を+ r * 2

def draw(i, from, len, rad, r, depth)

to = [
from[0] + len * Math.sin(rad),
from[1] + len * Math.cos(rad)
]

d = Magick::Draw.new
d.stroke("green")
d.line(from[1],from[0],to[1],to[0])
d.draw(i)

#p [from, to, depth]

if depth > 0
draw(i, to, len * 0.7, rad + r, r, depth - 1)
draw(i, to, len * 0.7, rad + r*2, r, depth - 1)
end
end

il = Magick::ImageList.new
il.delay = 50
for n in 1 .. 100
i = il.new_image(500,500)
i.annotate(Magick::Draw.new, 1, 1, 30, 30, n.to_s)
draw(i, [250,250], 50, Math::PI/-2, 0.1*n, 10)
end
i.write("tree4.gif")

tree4.gif