Sha256: 395705c4ca9770b7defcff7a78f8494d8f31f56f797ef326c5f3f2461c6c6da5

Contents?: true

Size: 897 Bytes

Versions: 10

Compression:

Stored size: 897 Bytes

Contents

module BoardGameGrid

  # = Direction
  #
  # The Direction that something is moving on a 2d plane
  class Direction

    # New objects can be instantiated with
    #
    # @param [Fixnum] dx
    #   the dx magnitude.
    #
    # @param [Fixnum] dy
    #   the dy magnitude.
    #
    # ==== Example:
    #   # Instantiates a new Direction
    #   BoardGameGrid::Direction.new(1, 1)
    def initialize(dx, dy)
      x = dx == 0 ? dx : dx/dx.abs
      y = dy == 0 ? dy : dy/dy.abs

      @x, @y = x, y
    end

    # @return [Fixnum] the x magnitude.
    attr_reader :x

    # @return [Fixnum] the y magnitude.
    attr_reader :y

    # Check if directions are equal by seeing if their magnitudes are equal.
    #
    # @param [Direction] other
    #   the other direction to compare to.
    #
    # @return [Boolean]
    def ==(other)
      self.x == other.x && self.y == other.y
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
board_game_grid-0.1.9 lib/board_game_grid/direction.rb
board_game_grid-0.1.8 lib/board_game_grid/direction.rb
board_game_grid-0.1.7 lib/board_game_grid/direction.rb
board_game_grid-0.1.6 lib/board_game_grid/direction.rb
board_game_grid-0.1.5 lib/board_game_grid/direction.rb
board_game_grid-0.1.4 lib/board_game_grid/direction.rb
board_game_grid-0.1.3 lib/board_game_grid/direction.rb
board_game_grid-0.1.2 lib/board_game_grid/direction.rb
board_game_grid-0.1.1 lib/board_game_grid/direction.rb
board_game_grid-0.1.0 lib/board_game_grid/direction.rb