Class: Amaze::Formatter::ASCII::Ortho

Inherits:
Amaze::Formatter::ASCII show all
Defined in:
lib/amaze/formatter/ascii/ortho.rb

Instance Attribute Summary

Attributes inherited from Amaze::Formatter::ASCII

#grid, #options

Instance Method Summary collapse

Methods inherited from Amaze::Formatter::ASCII

#ansi_clear, #cell_size, #char, #clear, #distance, #distance_color, #distances, #grid_color, #initialize, #path?, #path_cell?, #path_color, #render

Constructor Details

This class inherits a constructor from Amaze::Formatter::ASCII

Instance Method Details

#centerObject



98
99
100
# File 'lib/amaze/formatter/ascii/ortho.rb', line 98

def center
  ''
end

#char_array_heightObject



86
87
88
# File 'lib/amaze/formatter/ascii/ortho.rb', line 86

def char_array_height
  y(grid.rows) + 1
end

#char_array_widthObject



82
83
84
# File 'lib/amaze/formatter/ascii/ortho.rb', line 82

def char_array_width
  x(grid.columns) + 1
end

#coord(cell) ⇒ Object

left, right, top, bottom



70
71
72
# File 'lib/amaze/formatter/ascii/ortho.rb', line 70

def coord cell
  [x(cell.column), x(cell.column+1), y(cell.row), y(cell.row+1)]
end

#cornerObject



102
103
104
# File 'lib/amaze/formatter/ascii/ortho.rb', line 102

def corner
  '+'
end

#draw_cell(cell) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/amaze/formatter/ascii/ortho.rb', line 4

def draw_cell cell
  left, right, top, bottom = coord cell
  
  # corners
  char[top][left] = corner.color(grid_color)
  char[top][right] = corner.color(grid_color)
  char[bottom][left] = corner.color(grid_color)
  char[bottom][right] = corner.color(grid_color)
  # top & bottom
  (left+1).upto(right-1) do |i|
    # top
    char[top][i] = h.color(grid_color) unless cell.linked_to?(:north)
    # bottom
    char[bottom][i] = h.color(grid_color) unless cell.linked_to?(:south)
  end
  # left & right
  (top+1).upto(bottom-1) do |i|
    # left
    char[i][left] = v.color(grid_color) unless cell.linked_to?(:west)
    # right
    char[i][right] = v.color(grid_color) unless cell.linked_to?(:east)
  end
end

#draw_content(cell) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/amaze/formatter/ascii/ortho.rb', line 28

def draw_content cell
  left, right, top, bottom = coord cell

  my = top + cell_size / 2 + 1
  distance(cell).center(cell_size * 3).chars.each_with_index do |c,i|
    char[my][left+1+i] = c.color(*distance_color(cell))
  end
end

#draw_path(cell) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/amaze/formatter/ascii/ortho.rb', line 37

def draw_path cell
  left, right, top, bottom = coord cell

  mx = left + (cell_size * 3 + 1) / 2
  my = top + cell_size / 2 + 1
  
  # TODO: simplify paths, draw only north-south and east-west
  
  # to north
  top.upto(my-1) do |i|
    char[i][mx] = v.color(path_color) if path?(:north, cell)
  end if top <= my-1
  # to east
  (mx+1).upto(right) do |i|
    char[my][i] = h.color(path_color) if path?(:east, cell)
  end if mx+1 <= right
  # to south
  (my+1).upto(bottom) do |i|
    char[i][mx] = v.color(path_color) if path?(:south, cell)
  end if my+1 <= bottom
  # to west
  left.upto(mx-1) do |i|
    char[my][i] = h.color(path_color) if path?(:west, cell)
  end if left <= mx-1
  # center, select the char depeding on how many paths cross the cell
  center_char = center
  center_char = v if path?(:north, cell) && path?(:south, cell)
  center_char = h if path?(:east, cell) && path?(:west, cell)
  center_char = corner if [:north, :east, :south, :west].select{|d| path?(d, cell) }.size >= 3
  char[my][mx] = center_char.color(path_color)
end

#hObject



90
91
92
# File 'lib/amaze/formatter/ascii/ortho.rb', line 90

def h
  '-'
end

#vObject



94
95
96
# File 'lib/amaze/formatter/ascii/ortho.rb', line 94

def v
  '|'
end

#x(column) ⇒ Object



74
75
76
# File 'lib/amaze/formatter/ascii/ortho.rb', line 74

def x column
  (cell_size * 3 + 1) * column
end

#y(row) ⇒ Object



78
79
80
# File 'lib/amaze/formatter/ascii/ortho.rb', line 78

def y row
  (cell_size + 1) * row
end