Class: Vedeu::PositionIndex
- Inherits:
-
Object
- Object
- Vedeu::PositionIndex
- Defined in:
- lib/vedeu/geometry/position_index.rb
Overview
Converts a position into an index for the terminal. An index is the position minus 1.
Class Method Summary collapse
-
.[](y, x) ⇒ Object
Convenience constructor for Vedeu::Position.
Instance Method Summary collapse
-
#[] ⇒ Array<Fixnum>
Returns a tuple containing the y and x coordinates.
-
#eql?(other) ⇒ Boolean
(also: #==)
An object is equal when its values are the same.
-
#initialize(y, x) ⇒ Vedeu::PositionIndex
constructor
Returns a new instance of Vedeu::PositionIndex.
-
#to_position ⇒ Vedeu::Position
Converts the index values into a Vedeu::Position.
-
#x ⇒ Fixnum
(also: #last)
Returns the x index.
-
#y ⇒ Fixnum
(also: #first)
Returns the y index.
Constructor Details
#initialize(y, x) ⇒ Vedeu::PositionIndex
Returns a new instance of Vedeu::PositionIndex.
19 20 21 22 |
# File 'lib/vedeu/geometry/position_index.rb', line 19 def initialize(y, x) @y = y @x = x end |
Class Method Details
.[](y, x) ⇒ Object
Convenience constructor for Vedeu::Position.
10 11 12 |
# File 'lib/vedeu/geometry/position_index.rb', line 10 def self.[](y, x) new(y, x).[] end |
Instance Method Details
#[] ⇒ Array<Fixnum>
Returns a tuple containing the y and x coordinates.
27 28 29 |
# File 'lib/vedeu/geometry/position_index.rb', line 27 def [] [y, x] end |
#eql?(other) ⇒ Boolean Also known as: ==
An object is equal when its values are the same.
35 36 37 |
# File 'lib/vedeu/geometry/position_index.rb', line 35 def eql?(other) self.class == other.class && (x == other.x && y == other.y) end |
#to_position ⇒ Vedeu::Position
Converts the index values into a Vedeu::Position.
43 44 45 |
# File 'lib/vedeu/geometry/position_index.rb', line 43 def to_position Vedeu::Position.new(y, x) end |
#x ⇒ Fixnum Also known as: last
Returns the x index. If the position for x is less than 1, then the index is 0.
51 52 53 |
# File 'lib/vedeu/geometry/position_index.rb', line 51 def x @_x ||= ((@x - 1) <= 1) ? 0 : (@x - 1) end |
#y ⇒ Fixnum Also known as: first
Returns the y index. If the position for y is less than 1, then the index is 0.
60 61 62 |
# File 'lib/vedeu/geometry/position_index.rb', line 60 def y @_y ||= ((@y - 1) <= 1) ? 0 : (@y - 1) end |