Sha256: 66d8d20a798bec38f0f2082d185b250f8c46a3105529b91da3ce4e0a2c7c9be6

Contents?: true

Size: 1.75 KB

Versions: 7

Compression:

Stored size: 1.75 KB

Contents

require 'board_game_grid/direction'

module BoardGameGrid

  # = Vector
  #
  # An element of Vector space
  class Vector

    # New objects can be instantiated by passing in a two points with x and y co-ordinates
    #
    # @param [Point] origin
    #   the start point.
    #
    # @param [Point] destination
    #   the end point.
    #
    # ==== Example:
    #   # Instantiates a new Vector
    #   BoardGameGrid::Vector.new(
    #     BoardGameGrid::Point.new(x: 1, y: 1),
    #     BoardGameGrid::Point.new(x: 3, y: 3)
    #   )
    def initialize(origin, destination)
      @origin, @destination = origin, destination
    end

    # @return [Object] the origin.
    attr_reader :origin

    # @return [Object] the destination.
    attr_reader :destination

    # The direction of the vector as a object
    #
    # @return [Direction]
    def direction
      Direction.new(dx, dy)
    end

    # The biggest difference between co-ordinates
    #
    # @return [Fixnum]
    def magnitude
      [dx.abs, dy.abs].max
    end

    # Is the vector orthogonal?
    #
    # @return [Boolean]
    def orthogonal?
      dx == 0 || dy == 0
    end

    # Is the vector diagonal?
    #
    # @return [Boolean]
    def diagonal?
      dx.abs == dy.abs
    end

    # Is the vector not orthogonal or diagonal?
    #
    # @return [Boolean]
    def not_orthogonal_or_diagonal?
      !(orthogonal? || diagonal?)
    end

    # Is the vector orthogonal or diagonal?
    #
    # @return [Boolean]
    def orthogonal_or_diagonal?
      orthogonal? || diagonal?
    end

    # The distance on the x axis
    #
    # @return [Fixnum]
    def dx
      destination.x - origin.x
    end

    # The distance on the y axis
    #
    # @return [Fixnum]
    def dy
      destination.y - origin.y
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
board_game_grid-0.1.9 lib/board_game_grid/vector.rb
board_game_grid-0.1.8 lib/board_game_grid/vector.rb
board_game_grid-0.1.7 lib/board_game_grid/vector.rb
board_game_grid-0.1.6 lib/board_game_grid/vector.rb
board_game_grid-0.1.5 lib/board_game_grid/vector.rb
board_game_grid-0.1.4 lib/board_game_grid/vector.rb
board_game_grid-0.1.3 lib/board_game_grid/vector.rb