Class: Amaze::Formatter::Image::Delta

Inherits:
Amaze::Formatter::Image show all
Defined in:
lib/amaze/formatter/image/delta.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



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/amaze/formatter/image/delta.rb', line 95

def center_coord cell
  row, column = cell.row, cell.column
  
  x = (column+1) * pattern_width + cell_offset
  y0 = row * pattern_height + cell_offset
  
  dy = cell_width * Math.sqrt(3)
  fraction = (row+column).even? ? 6.0 : 3.0
  y = y0 + dy / fraction
  
  [x, y]
end

#coord(cell) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/amaze/formatter/image/delta.rb', line 79

def coord cell
  row, column = cell.row, cell.column

  x1 = column * pattern_width + cell_offset
  y1 = row * pattern_height + cell_offset
  x2 = x1 + pattern_width
  x3 = x2 + pattern_width
  y2 = y1 + pattern_height
  
  if (row+column).even?
    [x1, y1, x3, y1, x2, y2]
  else
    [x1, y2, x3, y2, x2, y1]
  end
end

#image_heightObject



120
121
122
# File 'lib/amaze/formatter/image/delta.rb', line 120

def image_height
  pattern_height * grid.rows + wall_width + border_width * 2 + 1
end

#image_widthObject



116
117
118
# File 'lib/amaze/formatter/image/delta.rb', line 116

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

#pattern_heightObject



112
113
114
# File 'lib/amaze/formatter/image/delta.rb', line 112

def pattern_height
  @pattern_height ||= cell_width * Math.sqrt(3) / 2.0
end

#pattern_widthObject



108
109
110
# File 'lib/amaze/formatter/image/delta.rb', line 108

def pattern_width
  @pattern_width ||= cell_width / 2.0
end

#render_backgroundObject



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/amaze/formatter/image/delta.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/delta.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



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

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 east ).each do |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
# File 'lib/amaze/formatter/image/delta.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 = coord cell
    
    canvas.line ax, ay, cx, cy unless cell.linked_to?(:west)
    canvas.line bx, by, cx, cy unless cell.linked_to?(:east)

    direction = (cell.row+cell.column).even? ? :north : :south
    canvas.line ax, ay, bx, by unless cell.linked_to?(direction)
  end
end