class Grid

The Grid class represents the grid. It is composed of nodes.

Public Class Methods

build_nodes(window, cols, rows, width, height) click to toggle source

Builds the nodes of the grid. It returns the list of nodes.

# File lib/astar_visualizer/grid.rb, line 19
def self.build_nodes(window, cols, rows, width, height)
  nodes = []
  rows.times do |y|
    nodes << []
    cols.times do |x|
      nodes[y] << Node.new(window, x, y, width, height)
    end
  end
  nodes
end
new(window, cols, rows, size) click to toggle source

Creates the grid with the nodes.

# File lib/astar_visualizer/grid.rb, line 33
def initialize(window, cols, rows, size)
  @window = window
  @cols = cols
  @rows = rows
  @width = size / cols    # width of a node's square
  @height = size / rows   # height of a node's square
  @nodes = Grid.build_nodes(window, cols, rows, @width, @height)
  @color = Gosu::Color.argb(0xaad3d3d3)
end

Public Instance Methods

draw() click to toggle source

Draws the nodes and the grid.

# File lib/astar_visualizer/grid.rb, line 120
def draw
  each_node(&:draw)
  draw_grid
end
draw_grid() click to toggle source

Draws the lines of the grid.

# File lib/astar_visualizer/grid.rb, line 100
def draw_grid
  @rows.times do |i|  # horizontal
    x1 = 0
    y1 = @height * i
    x2 = @width * @cols
    y2 = @height * i
    @window.draw_line(x1, y1, @color, x2, y2, @color)
  end
  @cols.times do |i|  # vertical
    x1 = @width * i
    y1 = 0
    x2 = @width * i
    y2 = @height * @rows
    @window.draw_line(x1, y1, @color, x2, y2, @color)
  end
end
each_node() { |node(x, y)| ... } click to toggle source

Yields all nodes of the grid.

# File lib/astar_visualizer/grid.rb, line 53
def each_node
  @rows.times do |y|
    @cols.times do |x|
      yield node(x, y)
    end
  end
end
find_node(mouse_x, mouse_y) click to toggle source

Finds a node in the grid using mouse position.

# File lib/astar_visualizer/grid.rb, line 128
def find_node(mouse_x, mouse_y)
  each_node do |node|
    return node if node.inside?(mouse_x, mouse_y)
  end
  nil
end
inside?(x, y) click to toggle source

Returns if the (x, y) position is in the grid.

# File lib/astar_visualizer/grid.rb, line 64
def inside?(x, y)
  x >= 0 && x < @cols && y >= 0 && y < @rows
end
node(x, y) click to toggle source

Returns a node from the grid.

# File lib/astar_visualizer/grid.rb, line 46
def node(x, y)
  @nodes[y][x]
end
reset!() click to toggle source

Resets all the nodes.

# File lib/astar_visualizer/grid.rb, line 93
def reset!
  each_node(&:reset!)
end
update_neighbors() click to toggle source

Updates the neighbors of all nodes.

# File lib/astar_visualizer/grid.rb, line 78
def update_neighbors
  each_node do |node|
    x = node.x
    y = node.y
    node.neighbors.clear
    node.add_to_neighbors(node(x, y - 1)) if walkable?(x, y - 1)  # ↑
    node.add_to_neighbors(node(x + 1, y)) if walkable?(x + 1, y)  # →
    node.add_to_neighbors(node(x, y + 1)) if walkable?(x, y + 1)  # ↓
    node.add_to_neighbors(node(x - 1, y)) if walkable?(x - 1, y)  # ←
  end
end
walkable?(x, y) click to toggle source

Returns if a node is walkable.

# File lib/astar_visualizer/grid.rb, line 71
def walkable?(x, y)
  inside?(x, y) && !node(x, y).obstacle?
end