Class: Amaze::Formatter::Image::Upsilon

Inherits:
Amaze::Formatter::Image show all
Defined in:
lib/amaze/formatter/image/upsilon.rb

Instance Attribute Summary

Attributes inherited from Amaze::Formatter::Image

#grid, #options

Instance Method Summary collapse

Methods inherited from Amaze::Formatter::Image

#background_color, #border_width, #canvas, #cell_offset, #cell_width, colors, #distance_color, #distances, #distances_max, #gradient, #hide_walls?, #image, #initialize, #path?, #path_cell?, #path_cells, #path_color, #path_finish, #path_start, #path_width, #render, #show_grid?, #wall_color, #wall_width, #write

Constructor Details

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

Instance Method Details

#center_coord(cell) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/amaze/formatter/image/upsilon.rb', line 113

def center_coord cell
  row, column = cell.row, cell.column
  
  d = partial_cell + cell_width / 2.0
  x = column * pattern_width + d + cell_offset
  y = row * pattern_width + d + cell_offset
  [x, y]
end

#coord(cell) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/amaze/formatter/image/upsilon.rb', line 93

def coord cell
  row, column = cell.row, cell.column
  
  x1 = column * pattern_width + cell_offset
  x2 = x1 + partial_cell
  x3 = x2 + cell_width
  x4 = x3 + partial_cell
  
  y1 = row * pattern_width + cell_offset
  y2 = y1 + partial_cell
  y3 = y2 + cell_width
  y4 = y3 + partial_cell

  if (row+column).even?
    [x2, y1, x3, y1, x4, y2, x4, y3, x3, y4, x2, y4, x1, y3, x1, y2]
  else
    [x2, y2, x3, y2, x3, y3, x2, y3]
  end
end

#image_widthObject Also known as: image_height



130
131
132
# File 'lib/amaze/formatter/image/upsilon.rb', line 130

def image_width
  pattern_width * (grid.columns) + partial_cell + wall_width + border_width * 2 + 1
end

#partial_cellObject



122
123
124
# File 'lib/amaze/formatter/image/upsilon.rb', line 122

def partial_cell
  @partial_cell ||= cell_width * Math.sqrt(2) / 2.0
end

#pattern_widthObject



126
127
128
# File 'lib/amaze/formatter/image/upsilon.rb', line 126

def pattern_width
  @pattern_width ||= cell_width + partial_cell
end

#render_backgroundObject



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/amaze/formatter/image/upsilon.rb', line 4

def render_background
  canvas.stroke_antialias true
  canvas.stroke_linecap 'round'
  canvas.stroke_linejoin 'round'
  canvas.stroke 'none'
  grid.each_cell do |cell|
    color = distance_color cell
    next unless color
    canvas.fill color
    canvas.polygon *coord(cell)
  end
end

#render_gridObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/amaze/formatter/image/upsilon.rb', line 17

def render_grid
  canvas.stroke_antialias true
  canvas.stroke_linecap 'round'
  canvas.stroke_linejoin 'round'
  canvas.stroke 'gray90'
  canvas.stroke_width 1
  canvas.fill 'none'

  grid.each_cell do |cell|
    canvas.polygon *coord(cell)
  end
end

#render_pathObject



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
# File 'lib/amaze/formatter/image/upsilon.rb', line 61

def render_path
  canvas.stroke_antialias true
  canvas.stroke_linecap 'round'
  canvas.stroke_linejoin 'round'
  canvas.fill 'none'
  canvas.stroke path_color
  canvas.stroke_width path_width
  
  grid.each_cell do |cell|
    next unless path_cell? cell

    x1, y1 = center_coord cell
    %i( north northeast east southeast ).each do |direction|
      # square cells don't respond to northeast and southeast
      next unless cell.respond_to? direction
      next unless path?(direction, cell)
      x2, y2 = center_coord cell.send(direction)
      canvas.line x1, y1, x2, y2
    end
  end

  # draw start and finish
  canvas.stroke_antialias true
  canvas.stroke_linecap 'round'
  canvas.fill path_color
  canvas.stroke 'none'
  [path_start, path_finish].compact.each do |cell|
    x, y = center_coord cell
    canvas.ellipse x, y, path_width*2, path_width*2, 0, 360
  end
end

#render_wallObject



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
56
57
58
59
# File 'lib/amaze/formatter/image/upsilon.rb', line 30

def render_wall
  canvas.stroke_antialias true
  canvas.stroke_linecap 'round'
  canvas.stroke_linejoin 'round'
  canvas.stroke wall_color
  canvas.stroke_width wall_width
  canvas.fill 'none'

  grid.each_cell do |cell|
    ax, ay, bx, by, cx, cy, dx, dy, ex, ey, fx, fy, gx, gy, hx, hy = coord cell
    
    if (cell.row + cell.column).even?
      # octo cells
      canvas.line ax, ay, bx, by unless cell.linked_to?(:north)
      canvas.line bx, by, cx, cy unless cell.linked_to?(:northeast)
      canvas.line cx, cy, dx, dy unless cell.linked_to?(:east)
      canvas.line dx, dy, ex, ey unless cell.linked_to?(:southeast)
      canvas.line ex, ey, fx, fy unless cell.linked_to?(:south)
      canvas.line fx, fy, gx, gy unless cell.linked_to?(:southwest)
      canvas.line gx, gy, hx, hy unless cell.linked_to?(:west)
      canvas.line hx, hy, ax, ay unless cell.linked_to?(:northwest)
    else
      # square cells
      canvas.line ax, ay, bx, by unless cell.linked_to?(:north)
      canvas.line bx, by, cx, cy unless cell.linked_to?(:east)
      canvas.line cx, cy, dx, dy unless cell.linked_to?(:south)
      canvas.line dx, dy, ax, ay unless cell.linked_to?(:west)
    end
  end
end