Class: Pixelflut::Canvas::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pixelflut/canvas/base.rb

Direct Known Subclasses

Buffered, Streamed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



8
9
10
# File 'lib/pixelflut/canvas/base.rb', line 8

def initialize
  clear!
end

Instance Attribute Details

#offset_xObject

Returns the value of attribute offset_x.



6
7
8
# File 'lib/pixelflut/canvas/base.rb', line 6

def offset_x
  @offset_x
end

#offset_yObject

Returns the value of attribute offset_y.



6
7
8
# File 'lib/pixelflut/canvas/base.rb', line 6

def offset_y
  @offset_y
end

Instance Method Details

#[]=(x, y, color) ⇒ Object



34
35
36
# File 'lib/pixelflut/canvas/base.rb', line 34

def []=(x, y, color)
  out("PX #{x(x)} #{y(y)} #{color}\n")
end

#ascii(x, y, dim = 10, color = @color, pic = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pixelflut/canvas/base.rb', line 74

def ascii(x, y, dim = 10, color = @color, pic = nil)
  pic ||= yield
  sx = x
  pic.each_line do |line|
    line.chomp!
    line.each_char do |c|
      rect(x, y, x + dim, y + dim, color) if ' ' != c && '_' != c
      x += dim
    end
    x = sx
    y += dim
  end
end

#clear!Object



12
13
14
15
# File 'lib/pixelflut/canvas/base.rb', line 12

def clear!
  @offset_x = @offset_y = 0
  @color = 'ffffffff'
end

#color(color) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/pixelflut/canvas/base.rb', line 26

def color(color)
  oc = @color
  @color = color
  yield
ensure
  @color = oc
end

#line(x1, y1, x2, y2, color = @color) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pixelflut/canvas/base.rb', line 46

def line(x1, y1, x2, y2, color = @color)
  return rect(x1, y1, x2, y2, color) if x1 == x2 || y1 == y2
  x, y, curpixel = x1, y1, 0
  deltax = (x2 - x1).abs
  deltay = (y2 - y1).abs
  xinc1 = xinc2 = x2 >= x1 ? 1 : -1
  yinc1 = yinc2 = y2 >= y1 ? 1 : -1
  if deltax >= deltay
    xinc1 = yinc2 = 0
    den, numadd, numpixels, num = deltax, deltay, deltax, deltax / 2
  else
    xinc2 = yinc1 = 0
    den, numadd, numpixels, num = deltay, deltax, deltay, deltay / 2
  end
  while curpixel <= numpixels
    num += numadd
    if num >= den
      num -= den
      x += xinc1
      y += yinc1
    end
    x += xinc2
    y += yinc2
    pix(x, y, color)
    curpixel += 1
  end
end

#pix(x, y, color = @color) ⇒ Object



38
39
40
# File 'lib/pixelflut/canvas/base.rb', line 38

def pix(x, y, color = @color)
  out("PX #{x(x)} #{y(y)} #{color}\n")
end

#rect(x1, y1, x2, y2, color = @color) ⇒ Object



42
43
44
# File 'lib/pixelflut/canvas/base.rb', line 42

def rect(x1, y1, x2, y2, color = @color)
  out("RC #{x(x1)} #{y(y1)} #{x(x2)} #{y(y2)} #{color}\n")
end

#translate(x, y) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/pixelflut/canvas/base.rb', line 17

def translate(x, y)
  ox, oy = @offset_x, @offset_y
  @offset_x += x
  @offset_y += y
  yield
ensure
  @offset_x, @offset_y = ox, oy
end