class Drawille::Brush

Attributes

canvas[RW]

Public Class Methods

new(canvas) click to toggle source
# File lib/drawille/brush.rb, line 8
def initialize canvas
  @canvas = canvas
  @state  = {
    x:        0, 
    y:        0,
    up:       true,
    rotation: 0
  }
end

Public Instance Methods

back(length) click to toggle source
# File lib/drawille/brush.rb, line 34
def back length
  forward -length
end
Also aliased as: bk
bk(length)
Alias for: back
down() click to toggle source
# File lib/drawille/brush.rb, line 18
def down
  @state[:up] = false
end
Also aliased as: pd
fd(length)
Alias for: forward
forward(length) click to toggle source
# File lib/drawille/brush.rb, line 26
def forward length
  theta = ((@state[:rotation]) / 180.0 * Math::PI)
  x     = (@state[:x] + length * Math::cos(theta)).round
  y     = (@state[:y] + length * Math::sin(theta)).round

  move x, y
end
Also aliased as: fd
left(angle) click to toggle source
# File lib/drawille/brush.rb, line 42
def left angle
  @state[:rotation] -= angle
end
Also aliased as: lt
line(coordinates={}) click to toggle source
# File lib/drawille/brush.rb, line 71
def line coordinates={}
  last_state = @state[:up]

  up
  move *coordinates[:from]
  down
  move *coordinates[:to]

  @state[:up] = last_state
end
lt(angle)
Alias for: left
move(x, y) click to toggle source
# File lib/drawille/brush.rb, line 46
def move x, y
  unless @state[:up]
    x1 = @state[:x].round
    y1 = @state[:y].round
    x2 = x
    y2 = y

    xdiff = [x1, x2].max - [x1, x2].min
    ydiff = [y1, y2].max - [y1, y2].min

    xdir = x1 <= x2 ? 1 : -1
    ydir = y1 <= y2 ? 1 : -1

    r = [xdiff, ydiff].max

    (0..r).each do |i|
      x, y = x1, y1
      y += (i.to_f*ydiff)/r*ydir if ydiff > 0
      x += (i.to_f*xdiff)/r*xdir if xdiff > 0
      @canvas.set(x, y)
    end
  end
  @state[:x], @state[:y] = x, y
end
Also aliased as: mv
mv(x, y)
Alias for: move
pd()
Alias for: down
pu()
Alias for: up
right(angle) click to toggle source
# File lib/drawille/brush.rb, line 38
def right angle
  @state[:rotation] += angle
end
Also aliased as: rt
rt(angle)
Alias for: right
up() click to toggle source
# File lib/drawille/brush.rb, line 22
def up
  @state[:up] = true
end
Also aliased as: pu