Class: BoardGameGrid::Vector
- Inherits:
-
Object
- Object
- BoardGameGrid::Vector
- Defined in:
- lib/board_game_grid/vector.rb
Overview
Vector
An element of Vector space
Instance Attribute Summary collapse
-
#destination ⇒ Object
readonly
The destination.
-
#origin ⇒ Object
readonly
The origin.
Instance Method Summary collapse
-
#diagonal? ⇒ Boolean
Is the vector diagonal?.
-
#direction ⇒ Direction
The direction of the vector as a object.
-
#dx ⇒ Fixnum
The distance on the x axis.
-
#dy ⇒ Fixnum
The distance on the y axis.
-
#initialize(origin, destination) ⇒ Vector
constructor
New objects can be instantiated by passing in a two points with x and y co-ordinates.
-
#magnitude ⇒ Fixnum
The biggest difference between co-ordinates.
-
#not_orthogonal_or_diagonal? ⇒ Boolean
Is the vector not orthogonal or diagonal?.
-
#orthogonal? ⇒ Boolean
Is the vector orthogonal?.
-
#orthogonal_or_diagonal? ⇒ Boolean
Is the vector orthogonal or diagonal?.
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)
)
24 25 26 |
# File 'lib/board_game_grid/vector.rb', line 24 def initialize(origin, destination) @origin, @destination = origin, destination end |
Instance Attribute Details
#destination ⇒ Object (readonly)
Returns the destination.
32 33 34 |
# File 'lib/board_game_grid/vector.rb', line 32 def destination @destination end |
#origin ⇒ Object (readonly)
Returns 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?
58 59 60 |
# File 'lib/board_game_grid/vector.rb', line 58 def diagonal? dx.abs == dy.abs end |
#direction ⇒ Direction
The direction of the vector as a object
37 38 39 |
# File 'lib/board_game_grid/vector.rb', line 37 def direction Direction.new(dx, dy) end |
#dx ⇒ Fixnum
The distance on the x axis
79 80 81 |
# File 'lib/board_game_grid/vector.rb', line 79 def dx destination.x - origin.x end |
#dy ⇒ Fixnum
The distance on the y axis
86 87 88 |
# File 'lib/board_game_grid/vector.rb', line 86 def dy destination.y - origin.y end |
#magnitude ⇒ Fixnum
The biggest difference between co-ordinates
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?
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?
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?
72 73 74 |
# File 'lib/board_game_grid/vector.rb', line 72 def orthogonal_or_diagonal? orthogonal? || diagonal? end |