Class: BoardGameGrid::Vector

Inherits:
Object
  • Object
show all
Defined in:
lib/board_game_grid/vector.rb

Overview

Vector

An element of Vector space

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin, destination) ⇒ Vector

New objects can be instantiated by passing in a two points with x and y co-ordinates

Example:

# Instantiates a new Vector
BoardGameGrid::Vector.new(
  BoardGameGrid::Point.new(x: 1, y: 1),
  BoardGameGrid::Point.new(x: 3, y: 3)
)

Parameters:

  • origin (Point)

    the start point.

  • destination (Point)

    the end point.



24
25
26
# File 'lib/board_game_grid/vector.rb', line 24

def initialize(origin, destination)
  @origin, @destination = origin, destination
end

Instance Attribute Details

#destinationObject (readonly)

Returns the destination.

Returns:

  • (Object)

    the destination.



32
33
34
# File 'lib/board_game_grid/vector.rb', line 32

def destination
  @destination
end

#originObject (readonly)

Returns the origin.

Returns:

  • (Object)

    the origin.



29
30
31
# File 'lib/board_game_grid/vector.rb', line 29

def origin
  @origin
end

Instance Method Details

#diagonal?Boolean

Is the vector diagonal?

Returns:

  • (Boolean)


58
59
60
# File 'lib/board_game_grid/vector.rb', line 58

def diagonal?
  dx.abs == dy.abs
end

#directionDirection

The direction of the vector as a object

Returns:



37
38
39
# File 'lib/board_game_grid/vector.rb', line 37

def direction
  Direction.new(dx, dy)
end

#dxFixnum

The distance on the x axis

Returns:

  • (Fixnum)


79
80
81
# File 'lib/board_game_grid/vector.rb', line 79

def dx
  destination.x - origin.x
end

#dyFixnum

The distance on the y axis

Returns:

  • (Fixnum)


86
87
88
# File 'lib/board_game_grid/vector.rb', line 86

def dy
  destination.y - origin.y
end

#magnitudeFixnum

The biggest difference between co-ordinates

Returns:

  • (Fixnum)


44
45
46
# File 'lib/board_game_grid/vector.rb', line 44

def magnitude
  [dx.abs, dy.abs].max
end

#not_orthogonal_or_diagonal?Boolean

Is the vector not orthogonal or diagonal?

Returns:

  • (Boolean)


65
66
67
# File 'lib/board_game_grid/vector.rb', line 65

def not_orthogonal_or_diagonal?
  !(orthogonal? || diagonal?)
end

#orthogonal?Boolean

Is the vector orthogonal?

Returns:

  • (Boolean)


51
52
53
# File 'lib/board_game_grid/vector.rb', line 51

def orthogonal?
  dx == 0 || dy == 0
end

#orthogonal_or_diagonal?Boolean

Is the vector orthogonal or diagonal?

Returns:

  • (Boolean)


72
73
74
# File 'lib/board_game_grid/vector.rb', line 72

def orthogonal_or_diagonal?
  orthogonal? || diagonal?
end