Class: Amaze::Formatter::Image::Sigma

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



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

def center_coord cell
  row, column = cell.row, cell.column
  
  x = column * pattern_width + cell_width + cell_offset
  # add half or full height to find center
  row_offset = column.even? ? 0.5 : 1.0
  y = (row+row_offset) * pattern_height  + cell_offset
  
  [x, y]
end

#coord(cell) ⇒ Object



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

def coord cell
  x, y = center_coord cell
  
  x1 = x - cell_width
  x2 = x - cell_width / 2.0
  x3 = x + cell_width / 2.0
  x4 = x + cell_width
  
  y1 = y - pattern_height / 2.0
  y2 = y
  y3 = y + pattern_height / 2.0

  [x1, x2, x3, x4, y1, y2, y3]
end

#image_heightObject



119
120
121
# File 'lib/amaze/formatter/image/sigma.rb', line 119

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

#image_widthObject



115
116
117
# File 'lib/amaze/formatter/image/sigma.rb', line 115

def image_width
  pattern_width * grid.columns + cell_width / 2.0 + wall_width + border_width * 2 + 1
end

#pattern_heightObject



111
112
113
# File 'lib/amaze/formatter/image/sigma.rb', line 111

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

#pattern_widthObject



107
108
109
# File 'lib/amaze/formatter/image/sigma.rb', line 107

def pattern_width
  @pattern_width ||= cell_width * 3 / 2.0
end

#render_backgroundObject



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

def render_background
  canvas.stroke_antialias true
  canvas.stroke_linecap 'square'
  canvas.stroke 'none'
  grid.each_cell do |cell|
    color = distance_color cell
    next unless color
    canvas.fill color
    x1, x2, x3, x4, y1, y2, y3 = coord cell
    canvas.polygon x2, y1, x3, y1, x4, y2, x3, y3, x2, y3, x1, y2
  end
end

#render_gridObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/amaze/formatter/image/sigma.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|
    x1, x2, x3, x4, y1, y2, y3 = coord cell
    canvas.polygon x2, y1, x3, y1, x4, y2, x3, y3, x2, y3, x1, y2
  end
end

#render_pathObject



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

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( northeast southeast south ).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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/amaze/formatter/image/sigma.rb', line 31

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|
    x1, x2, x3, x4, y1, y2, y3 = coord cell
    
    canvas.line x2, y1, x3, y1 unless cell.linked_to?(:north)
    canvas.line x3, y1, x4, y2 unless cell.linked_to?(:northeast)
    canvas.line x4, y2, x3, y3 unless cell.linked_to?(:southeast)
    canvas.line x2, y3, x3, y3 unless cell.linked_to?(:south)
    canvas.line x1, y2, x2, y3 unless cell.linked_to?(:southwest)
    canvas.line x1, y2, x2, y1 unless cell.linked_to?(:northwest)
  end
end