Sha256: 4ffb89ae16a56a84cbd0a43ed27380bc583ffff4b13645f6b528767ab295b55f

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

require 'just_chess/direction'

module JustChess

  # = 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
    #   JustChess::Vector.new(
    #     Struct.new(:x, :y).new(1, 1),
    #     Struct.new(:x, :y).new(3, 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

    private

    def dx
      destination.x - origin.x
    end

    def dy
      destination.y - origin.y
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
just_chess-0.1.0 lib/just_chess/vector.rb