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.1-x86-mingw32 samples/tree.rb
smalruby-0.1.1 samples/tree.rb
smalruby-0.1.0-x86-mingw32 samples/tree.rb
smalruby-0.1.0 samples/tree.rb
smalruby-0.0.32-x86-mingw32 samples/tree.rb
smalruby-0.0.32 samples/tree.rb
smalruby-0.0.31-x86-mingw32 samples/tree.rb
smalruby-0.0.31 samples/tree.rb
smalruby-0.0.30-x86-mingw32 samples/tree.rb
smalruby-0.0.30 samples/tree.rb
smalruby-0.0.29-x86-mingw32 samples/tree.rb
smalruby-0.0.29 samples/tree.rb
smalruby-0.0.28-x86-mingw32 samples/tree.rb
smalruby-0.0.28 samples/tree.rb
smalruby-0.0.27-x86-mingw32 samples/tree.rb
smalruby-0.0.27 samples/tree.rb
smalruby-0.0.26-x86-mingw32 samples/tree.rb
smalruby-0.0.26 samples/tree.rb
smalruby-0.0.25-x86-mingw32 samples/tree.rb
smalruby-0.0.25 samples/tree.rb