Sha256: 55e59e9b8d1dcb0106e404408e66a019db0a6d6a2407c858700c1a71b07d75fb

Contents?: true

Size: 919 Bytes

Versions: 16

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: "white")
  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

16 entries across 16 versions & 1 rubygems

Version Path
smalruby-0.0.22-x86-mingw32 samples/tree.rb
smalruby-0.0.22 samples/tree.rb
smalruby-0.0.21-x86-mingw32 samples/tree.rb
smalruby-0.0.21 samples/tree.rb
smalruby-0.0.20-x86-mingw32 samples/tree.rb
smalruby-0.0.20 samples/tree.rb
smalruby-0.0.19-x86-mingw32 samples/tree.rb
smalruby-0.0.19 samples/tree.rb
smalruby-0.0.18-x86-mingw32 samples/tree.rb
smalruby-0.0.18 samples/tree.rb
smalruby-0.0.17-x86-mingw32 samples/tree.rb
smalruby-0.0.17 samples/tree.rb
smalruby-0.0.16-x86-mingw32 samples/tree.rb
smalruby-0.0.16 samples/tree.rb
smalruby-0.0.15-x86-mingw32 samples/tree.rb
smalruby-0.0.15 samples/tree.rb