class BoardGameGrid::Vector

Vector

An element of Vector space

Attributes

destination[R]

@return [Object] the destination.

origin[R]

@return [Object] the origin.

Public Class Methods

new(origin, destination) click to toggle source

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)
)
# File lib/board_game_grid/vector.rb, line 24
def initialize(origin, destination)
  @origin, @destination = origin, destination
end

Public Instance Methods

diagonal?() click to toggle source

Is the vector diagonal?

@return [Boolean]

# File lib/board_game_grid/vector.rb, line 58
def diagonal?
  dx.abs == dy.abs
end
direction() click to toggle source

The direction of the vector as a object

@return [Direction]

# File lib/board_game_grid/vector.rb, line 37
def direction
  Direction.new(dx, dy)
end
dx() click to toggle source

The distance on the x axis

@return [Fixnum]

# File lib/board_game_grid/vector.rb, line 79
def dx
  destination.x - origin.x
end
dy() click to toggle source

The distance on the y axis

@return [Fixnum]

# File lib/board_game_grid/vector.rb, line 86
def dy
  destination.y - origin.y
end
magnitude() click to toggle source

The biggest difference between co-ordinates

@return [Fixnum]

# File lib/board_game_grid/vector.rb, line 44
def magnitude
  [dx.abs, dy.abs].max
end
not_orthogonal_or_diagonal?() click to toggle source

Is the vector not orthogonal or diagonal?

@return [Boolean]

# File lib/board_game_grid/vector.rb, line 65
def not_orthogonal_or_diagonal?
  !(orthogonal? || diagonal?)
end
orthogonal?() click to toggle source

Is the vector orthogonal?

@return [Boolean]

# File lib/board_game_grid/vector.rb, line 51
def orthogonal?
  dx == 0 || dy == 0
end
orthogonal_or_diagonal?() click to toggle source

Is the vector orthogonal or diagonal?

@return [Boolean]

# File lib/board_game_grid/vector.rb, line 72
def orthogonal_or_diagonal?
  orthogonal? || diagonal?
end