Class: Amaze::Formatter::ASCII::Polar

Inherits:
Amaze::Formatter::ASCII show all
Defined in:
lib/amaze/formatter/ascii/polar.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



166
167
168
# File 'lib/amaze/formatter/ascii/polar.rb', line 166

def center
  ''
end

#center_coord(cell) ⇒ Object



132
133
134
135
# File 'lib/amaze/formatter/ascii/polar.rb', line 132

def center_coord cell
  x1, x2, y1, y2 = coord cell
  [(x1 + x2) / 2, (y1 + y2) / 2]
end

#char_array_heightObject



154
155
156
# File 'lib/amaze/formatter/ascii/polar.rb', line 154

def char_array_height
  y(grid.rows) + 1
end

#char_array_widthObject



150
151
152
# File 'lib/amaze/formatter/ascii/polar.rb', line 150

def char_array_width
  x(max_columns) + 1
end

#coord(cell) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/amaze/formatter/ascii/polar.rb', line 122

def coord cell
  columns = grid.columns(cell.row).size
  x2 = char_array_width - 1 - x(cell.column, columns)
  x1 = char_array_width - 1 - x(cell.column+1, columns)
  y1 = y(cell.row)
  y2 = y(cell.row+1)
  
  [x1, x2, y1, y2]
end

#cornerObject



170
171
172
# File 'lib/amaze/formatter/ascii/polar.rb', line 170

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/polar.rb', line 4

def draw_cell cell
  x1, x2, y1, y2 = coord cell
  
  # corners
  char[y1][x1] = corner.color(grid_color)
  char[y1][x2] = corner.color(grid_color)
  char[y2][x1] = corner.color(grid_color)
  char[y2][x2] = corner.color(grid_color)
  # top & bottom
  (x1+1).upto(x2-1) do |i|
    # top (inward)
    char[y1][i] = h.color(grid_color) unless cell.linked_to?(:inward)
    # bottom (outward)
    char[y2][i] = h.color(grid_color) if cell.row == grid.rows-1
  end
  # left & right
  (y1+1).upto(y2-1) do |i|
    # left (cw)
    char[i][x1] = v.color(grid_color) unless cell.linked_to?(:cw)
    # right (ccw)
    char[i][x2] = v.color(grid_color) unless cell.linked_to?(:ccw)
  end
end

#draw_content(cell) ⇒ Object



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

def draw_content cell
  x1, x2, y1, y2 = coord cell
  _, my = center_coord cell
  dx = x2 - x1 - 1 

  distance(cell).center(dx).chars.each_with_index do |c,i|
    char[my][x1+1+i] = c.color(*distance_color(cell))
  end
end

#draw_path(cell) ⇒ Object



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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/amaze/formatter/ascii/polar.rb', line 38

def draw_path cell
  # draw horizontal connections in cells with more than one outward path
  if outward_subdivided?(cell) && ! path?(:cw, cell) && ! path?(:ccw, cell)
    outward_cells = path_outward(cell)
    if outward_cells.any?
      mx, my = center_coord cell
      x_outward_cells = outward_cells.map{|c| x,_ = center_coord c; x}
      mx = x_outward_cells.first if cell.row.zero?
      mx_min = [mx, *x_outward_cells].min
      mx_max = [mx, *x_outward_cells].max
      char[my][mx_min] = center.color(path_color)
      char[my][mx_max] = center.color(path_color)
      (mx_min+1).upto(mx_max-1) do |i|
        char[my][i] = h.color(path_color)
      end
    end
  end
  
  # to cw
  if path?(:cw, cell)
    mx1, my1 = center_coord cell.cw
    mx2, my2 = center_coord cell
    if outward_subdivided? cell
      outward_cells_cw = path_outward cell.cw
      mx1, _ = center_coord(outward_cells_cw.first) if outward_cells_cw.any?
      outward_cells = path_outward cell
      mx2, _ = center_coord(outward_cells.first) if outward_cells.any?
    end
    mx1 = -1 if mx1 > mx2
    (mx1+1).upto(mx2-1) do |i|
      char[my1][i] = h.color(path_color)
    end
    char[my1][mx2] = center.color(path_color)
  end
  
  # to ccw
  if path?(:ccw, cell)
    mx1, my1 = center_coord cell
    mx2, my2 = center_coord cell.ccw
    if outward_subdivided? cell
      outward_cells = path_outward cell
      mx1, _ = center_coord(outward_cells.first) if outward_cells.any?
      outward_cells_ccw = path_outward cell.ccw
      mx2, _ = center_coord(outward_cells_ccw.first) if outward_cells_ccw.any?
    end
    mx2 = char_array_width if mx1 > mx2
    (mx1+1).upto(mx2-1) do |i|
      char[my1][i] = h.color(path_color)
    end
    char[my1][mx1] = center.color(path_color)
  end
  
  # to inward
  if path?(:inward, cell)
    mx, my = center_coord cell
    v1, v2, w1, w2 = coord cell.inward
    mw = (w1 + w2) / 2
    (mw+1).upto(my-1) do |i|
      char[i][mx] = v.color(path_color)
    end
  end

  # center
  mx, my = center_coord cell
  if outward_subdivided? cell
    char[my][mx] = h.color(path_color) if path?(:ccw, cell) && path?(:cw, cell)
    char[my][mx] = center.color(path_color) if path?(:inward, cell) && path_outward(cell).empty?
  else
    center_char = center
    center_char = h if path?(:ccw, cell) && path?(:cw, cell)
    center_char = v if path?(:inward, cell) && path_outward(cell).any?
    char[my][mx] = center_char.color(path_color)
  end
end

#hObject



158
159
160
# File 'lib/amaze/formatter/ascii/polar.rb', line 158

def h
  '-'
end

#max_columnsObject



146
147
148
# File 'lib/amaze/formatter/ascii/polar.rb', line 146

def max_columns
  grid.columns(grid.rows-1).size
end

#outward_subdivided?(cell) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
# File 'lib/amaze/formatter/ascii/polar.rb', line 113

def outward_subdivided? cell
  return false if grid.rows == cell.row + 1
  grid.columns(cell.row).size != grid.columns(cell.row+1).size
end

#path_outward(cell) ⇒ Object



118
119
120
# File 'lib/amaze/formatter/ascii/polar.rb', line 118

def path_outward cell
  cell.outward.select {|o| cell.linked?(o) && path_cell?(o) }
end

#vObject



162
163
164
# File 'lib/amaze/formatter/ascii/polar.rb', line 162

def v
  '|'
end

#x(column, columns = max_columns) ⇒ Object



137
138
139
140
# File 'lib/amaze/formatter/ascii/polar.rb', line 137

def x column, columns=max_columns
  factor = max_columns / columns
  (cell_size * 4 * factor) * column
end

#y(row) ⇒ Object



142
143
144
# File 'lib/amaze/formatter/ascii/polar.rb', line 142

def y row
  (cell_size + 1) * row
end