Class: Vedeu::PositionIndex

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(y, x) ⇒ Vedeu::PositionIndex

Returns a new instance of Vedeu::PositionIndex.

Parameters:

  • y (Fixnum)
  • x (Fixnum)


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.

Parameters:

  • y (Fixnum)
  • x (Fixnum)


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.

Returns:

  • (Array<Fixnum>)


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.

Parameters:

Returns:

  • (Boolean)


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_positionVedeu::Position

Converts the index values into a Vedeu::Position.

Returns:



43
44
45
# File 'lib/vedeu/geometry/position_index.rb', line 43

def to_position
  Vedeu::Position.new(y, x)
end

#xFixnum Also known as: last

Returns the x index. If the position for x is less than 1, then the index is 0.

Returns:

  • (Fixnum)


51
52
53
# File 'lib/vedeu/geometry/position_index.rb', line 51

def x
  @_x ||= ((@x - 1) <= 1) ? 0 : (@x - 1)
end

#yFixnum Also known as: first

Returns the y index. If the position for y is less than 1, then the index is 0.

Returns:

  • (Fixnum)


60
61
62
# File 'lib/vedeu/geometry/position_index.rb', line 60

def y
  @_y ||= ((@y - 1) <= 1) ? 0 : (@y - 1)
end