class Node

The Node class represents the squares of the grid.

Attributes

neighbors[R]

Gets the neighbors (list of nodes).

x[R]

Gets the x position in the grid.

y[R]

Gets the y position in the grid.

Public Class Methods

new(window, x, y, width, height) click to toggle source

Creates a node. It is composed of the (x, y) position in the grid and the neighbors (list of nodes).

# File lib/astar_visualizer/node.rb, line 28
def initialize(window, x, y, width, height)
  @@colors ||= {
    green: Gosu::Color.argb(0xff00ff00),
    red: Gosu::Color.argb(0xffff0000),
    grey: Gosu::Color.argb(0xff808080),
    lightcyan: Gosu::Color.argb(0xffe0ffff),
    cyan: Gosu::Color.argb(0xff00ffff),
    white: Gosu::Color.argb(0xffffffff),
    yellow: Gosu::Color.argb(0xffffff00),
  }
  @@window ||= window
  @x = x
  @y = y
  @width = width
  @height = height
  @color = @@colors[:white]
  @neighbors = []
end

Public Instance Methods

add_to_neighbors(node) click to toggle source

Adds a node to the neighbors list.

# File lib/astar_visualizer/node.rb, line 151
def add_to_neighbors(node)
  @neighbors << node
end
closed!() click to toggle source

Makes a node like in the closed list (lightcyan color).

# File lib/astar_visualizer/node.rb, line 113
def closed!
  @color = @@colors[:lightcyan]
end
closed?() click to toggle source

Returns if the node is in the closed list (lightcyan color).

# File lib/astar_visualizer/node.rb, line 106
def closed?
  @color == @@colors[:lightcyan]
end
draw() click to toggle source

Draws the square.

# File lib/astar_visualizer/node.rb, line 134
def draw
  @@window.draw_rect(@x * @width, @y * @height, @width, @height, @color)
end
end!() click to toggle source

Makes a node the end point (red color).

# File lib/astar_visualizer/node.rb, line 71
def end!
  @color = @@colors[:red]
end
end?() click to toggle source

Returns if the node is the end point (red color).

# File lib/astar_visualizer/node.rb, line 64
def end?
  @color == @@colors[:red]
end
inside?(mouse_x, mouse_y) click to toggle source

Returns if the mouse position is in the square.

# File lib/astar_visualizer/node.rb, line 141
def inside?(mouse_x, mouse_y)
  pos_x = @x * @width
  pos_y = @y * @height
  mouse_x >= pos_x && mouse_x <= pos_x + @width && \
    mouse_y >= pos_y && mouse_y <= pos_y + @height
end
obstacle!() click to toggle source

Makes a node an obstacle (grey color).

# File lib/astar_visualizer/node.rb, line 85
def obstacle!
  @color = @@colors[:grey]
end
obstacle?() click to toggle source

Returns if the node is an obstacle (grey color).

# File lib/astar_visualizer/node.rb, line 78
def obstacle?
  @color == @@colors[:grey]
end
open!() click to toggle source

Makes a node like in the open list (cyan color).

# File lib/astar_visualizer/node.rb, line 99
def open!
  @color = @@colors[:cyan]
end
open?() click to toggle source

Returns if the node is in the open list (cyan color).

# File lib/astar_visualizer/node.rb, line 92
def open?
  @color == @@colors[:cyan]
end
path!() click to toggle source

Makes a node in the found path (yellow color).

# File lib/astar_visualizer/node.rb, line 127
def path!
  @color = @@colors[:yellow]
end
reset!() click to toggle source

Resets a node (white color).

# File lib/astar_visualizer/node.rb, line 120
def reset!
  @color = @@colors[:white]
end
start!() click to toggle source

Makes a node the start point (green color).

# File lib/astar_visualizer/node.rb, line 57
def start!
  @color = @@colors[:green]
end
start?() click to toggle source

Returns if the node is the start point (green color).

# File lib/astar_visualizer/node.rb, line 50
def start?
  @color == @@colors[:green]
end