Class: Drawille::Brush
- Inherits:
-
Object
- Object
- Drawille::Brush
- Defined in:
- lib/drawille/brush.rb
Instance Attribute Summary collapse
-
#canvas ⇒ Object
Returns the value of attribute canvas.
Instance Method Summary collapse
- #back(length) ⇒ Object (also: #bk)
- #down ⇒ Object (also: #pd)
- #forward(length) ⇒ Object (also: #fd)
-
#initialize(canvas) ⇒ Brush
constructor
A new instance of Brush.
- #left(angle) ⇒ Object (also: #lt)
- #line(coordinates = {}) ⇒ Object
- #move(x, y) ⇒ Object (also: #mv)
- #right(angle) ⇒ Object (also: #rt)
- #up ⇒ Object (also: #pu)
Constructor Details
#initialize(canvas) ⇒ Brush
Returns a new instance of Brush.
9 10 11 12 13 14 15 16 17 |
# File 'lib/drawille/brush.rb', line 9 def initialize canvas @canvas = canvas @state = { x: 0, y: 0, up: true, rotation: 0 } end |
Instance Attribute Details
#canvas ⇒ Object
Returns the value of attribute canvas.
7 8 9 |
# File 'lib/drawille/brush.rb', line 7 def canvas @canvas end |
Instance Method Details
#back(length) ⇒ Object Also known as: bk
35 36 37 |
# File 'lib/drawille/brush.rb', line 35 def back length forward -length end |
#down ⇒ Object Also known as: pd
19 20 21 |
# File 'lib/drawille/brush.rb', line 19 def down @state[:up] = false end |
#forward(length) ⇒ Object Also known as: fd
27 28 29 30 31 32 33 |
# File 'lib/drawille/brush.rb', line 27 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 |
#left(angle) ⇒ Object Also known as: lt
43 44 45 |
# File 'lib/drawille/brush.rb', line 43 def left angle @state[:rotation] -= angle end |
#line(coordinates = {}) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/drawille/brush.rb', line 72 def line coordinates={} last_state = @state[:up] up move *coordinates[:from] down move *coordinates[:to] @state[:up] = last_state end |
#move(x, y) ⇒ Object Also known as: mv
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/drawille/brush.rb', line 47 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 |
#right(angle) ⇒ Object Also known as: rt
39 40 41 |
# File 'lib/drawille/brush.rb', line 39 def right angle @state[:rotation] += angle end |
#up ⇒ Object Also known as: pu
23 24 25 |
# File 'lib/drawille/brush.rb', line 23 def up @state[:up] = true end |