Sha256: fee2bea7ad557e8e595ab144a4ab7ff5da668b35c99770573696dd1d5f5099b7

Contents?: true

Size: 919 Bytes

Versions: 44

Compression:

Stored size: 919 Bytes

Contents

# -*- coding: utf-8 -*-
require 'smalruby'

# @author: Akihiro Sada
# @license: MIT Lisence
class Tree
  def initialize(canvas)
    @x = 320
    @y = 480
    @radian = -Math::PI / 2
    @canvas = canvas
  end

  def draw(level)
    if level <= 0
      return
    end
    length = level * 5
    forward(length)
    degree = 20
    turn(degree)
    draw(level - 1)
    turn(-degree * 2)
    draw(level - 1)
    turn(degree)
    backward(length)
  end

  private

  def forward(length)
    x = @x
    y = @y
    @x += Math.cos(@radian) * length
    @y += Math.sin(@radian) * length
    @canvas.line(x1: x, y1: y, x2: @x, y2: @y, color: "green")
  end

  def backward(length)
    @x -= Math.cos(@radian) * length
    @y -= Math.sin(@radian) * length
  end

  def turn(degree)
    @radian += degree * Math::PI / 180
  end
end

background = Canvas.new
tree = Tree.new(background)
background.on(:start) do
  tree.draw(12)
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
smalruby-0.1.11-x86-mingw32 samples/tree.rb
smalruby-0.1.11 samples/tree.rb
smalruby-0.1.10-x86-mingw32 samples/tree.rb
smalruby-0.1.10 samples/tree.rb
smalruby-0.1.9-x86-mingw32 samples/tree.rb
smalruby-0.1.9 samples/tree.rb
smalruby-0.1.8-x86-mingw32 samples/tree.rb
smalruby-0.1.8 samples/tree.rb
smalruby-0.1.7-x86-mingw32 samples/tree.rb
smalruby-0.1.7 samples/tree.rb
smalruby-0.1.6-x86-mingw32 samples/tree.rb
smalruby-0.1.6 samples/tree.rb
smalruby-0.1.5-x86-mingw32 samples/tree.rb
smalruby-0.1.5 samples/tree.rb
smalruby-0.1.4-x86-mingw32 samples/tree.rb
smalruby-0.1.4 samples/tree.rb
smalruby-0.1.3-x86-mingw32 samples/tree.rb
smalruby-0.1.3 samples/tree.rb
smalruby-0.1.2-x86-mingw32 samples/tree.rb
smalruby-0.1.2 samples/tree.rb