Class: Diagrams::Grid

Inherits:
Object show all
Defined in:
lib/maruku/ext/diagrams/grid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, init) ⇒ Grid

Returns a new instance of Grid.



6
7
8
9
10
11
# File 'lib/maruku/ext/diagrams/grid.rb', line 6

def initialize(width, height, init)
	@width, @height =  width, height
	@data = Array.new
	height.times do @data.push [init]*@width end
	@init = init
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



4
5
6
# File 'lib/maruku/ext/diagrams/grid.rb', line 4

def height
  @height
end

#widthObject

Returns the value of attribute width.



4
5
6
# File 'lib/maruku/ext/diagrams/grid.rb', line 4

def width
  @width
end

Instance Method Details

#eachObject



49
50
51
52
53
54
55
56
# File 'lib/maruku/ext/diagrams/grid.rb', line 49

def each
	for y in 0..(height-1)
		for x in 0..(width-1)
			e = get(x,y)
			yield x,y,e
		end
	end
end

#get(x, y) ⇒ Object



13
14
15
# File 'lib/maruku/ext/diagrams/grid.rb', line 13

def get(x, y)
	@data[y][x]
end

#inspectObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/maruku/ext/diagrams/grid.rb', line 21

def inspect
	@data.map{|x| 
		x.map{|y| 
			case y
			when true;  '#'
			when false, nil; ' '
			when Fixnum; y.chr
			end
		}.join
	}.join("\n")
end

#inspect2(valid) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/maruku/ext/diagrams/grid.rb', line 33

def inspect2(valid)
	s = ""
	
	s += "+" + "-"*@width + "+\n"
	for y in 0..(@height-1)
		s += "|"
		for x in 0..(@width-1)
			s += valid.get(x,y) ? get(x,y).chr : " "
		end
		s += "|"
		s += "\n"
	end
	s += "+" + "-"*@width + "+\n"
	s
end

#read_area(x, y, w, h) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/maruku/ext/diagrams/grid.rb', line 66

def read_area(x,y,w,h)
	s = ""
	for j in (y..y+h-1)
		for i in (x..x+w-1)
			s << get(i,j)
		end
		s << "\n"
	end
	s
end

#set(x, y, v) ⇒ Object



17
18
19
# File 'lib/maruku/ext/diagrams/grid.rb', line 17

def set(x, y, v)
	@data[y][x] = v || @init
end

#set_area(x, y, w, h, e) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/maruku/ext/diagrams/grid.rb', line 58

def set_area(x,y,w,h,e)
	for i in (x..x+w-1)
		for j in (y..y+h-1)
			set(i,j,e)
		end
	end
end