class Node
The Node
class represents the squares of the grid.
Attributes
Gets the neighbors (list of nodes).
Gets the x position in the grid.
Gets the y position in the grid.
Public Class Methods
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
Adds a node to the neighbors list.
# File lib/astar_visualizer/node.rb, line 151 def add_to_neighbors(node) @neighbors << node end
Makes a node like in the closed list (lightcyan color).
# File lib/astar_visualizer/node.rb, line 113 def closed! @color = @@colors[:lightcyan] end
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
Draws the square.
# File lib/astar_visualizer/node.rb, line 134 def draw @@window.draw_rect(@x * @width, @y * @height, @width, @height, @color) end
Makes a node the end point (red color).
# File lib/astar_visualizer/node.rb, line 71 def end! @color = @@colors[:red] end
Returns if the node is the end point (red color).
# File lib/astar_visualizer/node.rb, line 64 def end? @color == @@colors[:red] end
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
Makes a node an obstacle (grey color).
# File lib/astar_visualizer/node.rb, line 85 def obstacle! @color = @@colors[:grey] end
Returns if the node is an obstacle (grey color).
# File lib/astar_visualizer/node.rb, line 78 def obstacle? @color == @@colors[:grey] end
Makes a node like in the open list (cyan color).
# File lib/astar_visualizer/node.rb, line 99 def open! @color = @@colors[:cyan] end
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
Makes a node in the found path (yellow color).
# File lib/astar_visualizer/node.rb, line 127 def path! @color = @@colors[:yellow] end
Resets a node (white color).
# File lib/astar_visualizer/node.rb, line 120 def reset! @color = @@colors[:white] end
Makes a node the start point (green color).
# File lib/astar_visualizer/node.rb, line 57 def start! @color = @@colors[:green] end
Returns if the node is the start point (green color).
# File lib/astar_visualizer/node.rb, line 50 def start? @color == @@colors[:green] end