Class: Graphit::BitmapDrawing

Inherits:
Object
  • Object
show all
Defined in:
lib/graphit/bitmap_drawing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(height, width, background_color = [0x00,0x00,0x00]) ⇒ BitmapDrawing

Returns a new instance of BitmapDrawing.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/graphit/bitmap_drawing.rb', line 6

def initialize( height, width, background_color = [0x00,0x00,0x00] )
  background_color = background_color.to_hex_array if background_color.class == Color
  
  self.pixels = []
  (1..height).each do |y|
    row = []
    (1..width).each do |x|
      row.push( background_color )
    end
    self.pixels.push(row)
  end
end

Instance Attribute Details

#pixelsObject

Returns the value of attribute pixels.



4
5
6
# File 'lib/graphit/bitmap_drawing.rb', line 4

def pixels
  @pixels
end

Instance Method Details

#draw_line(start_point, end_point, color) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/graphit/bitmap_drawing.rb', line 57

def draw_line( start_point, end_point, color )
  color = color.to_hex_array if color.class == Color
  
  len = Math.sqrt( (end_point.x-start_point.x)**2 + (end_point.y - start_point.y)**2 )
  
  d = 0.0
  
  while d < 1
    if end_point.x == start_point.x
      x = start_point.x
    else
      x = start_point.x + (end_point.x-start_point.x) * d
    end

    y = start_point.y + (end_point.y-start_point.y) * d

    self.pixels[y.to_i][x.to_i] = color unless self.pixels[y.to_i][x.to_i].nil?

    d += 1.0 / ( len * 2.0 )
  end
  
  return self.pixels
end

#draw_text(text, point, color = [0x00,0x00,0x00]) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/graphit/bitmap_drawing.rb', line 27

def draw_text( text, point, color = [0x00,0x00,0x00] )
  @characters = Graphit.pixel_font
  color = color.to_hex_array if color.class == Color
  
  px = point.x
  
  text.each_char do |c|
    if @characters[c].nil?
      # skip
    else
      @characters[c].each_with_index do |row, y|
        row.each_with_index do |col, x|
          if col == 0
            # Do nothing
          else
            thisY = px + x
            thisX = point.y + y
        
            self.pixels[thisX][thisY] = color unless pixels[thisX][thisY].nil?
          end
        end
      end
  
      px += @characters[c].size + 1
    end
  end
  
  return self.pixels
end

#heightObject



19
20
21
# File 'lib/graphit/bitmap_drawing.rb', line 19

def height
  self.pixels.size rescue 0
end

#widthObject



23
24
25
# File 'lib/graphit/bitmap_drawing.rb', line 23

def width
  self.pixels[0].size rescue 0
end