Class: Spreet::Coordinates
- Inherits:
-
Object
- Object
- Spreet::Coordinates
- Defined in:
- lib/spreet/coordinates.rb
Overview
This class permit to manipulate coordinates in a table
Constant Summary collapse
- X_BIT_SHIFT =
Limit coordinates x and y in 0..65535 but coordinates are in one integer of 32 bits
20
- Y_FILTER =
2²⁰ rows = 1_048_576, 2¹² cols = 4_096,
((1 << X_BIT_SHIFT) - 1).freeze
- BASE_26_BEF =
"0123456789abcdefghijklmnop"
- BASE_26_AFT =
"abcdefghijklmnopqrstuvwxyz"
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #<=>(other_coordinate) ⇒ Object
- #==(other_coordinate) ⇒ Object
-
#initialize(*args) ⇒ Coordinates
constructor
A new instance of Coordinates.
- #to_a ⇒ Object
- #to_hash ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*args) ⇒ Coordinates
Returns a new instance of Coordinates.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/spreet/coordinates.rb', line 15 def initialize(*args) value = (args.size == 1 ? args[0] : args) @x, @y = 0, 0 if value.is_a? String if value.downcase.match(/^[a-z]+[0-9]+$/) value = value.downcase.split(/([A-Z]+|[0-9]+)/).delete_if{|x| x.size.zero?} @x, @y = value[0].tr(BASE_26_AFT, BASE_26_BEF).to_i(26), value[1].to_i(10)-1 elsif value.downcase.match(/^[0-9]+[^0-9]+[0-9]+$/) value = value.downcase.split(/[^0-9]+/) @x, @y = value[0].to_i(10), value[1].to_i(10) end elsif value.is_a? Integer @x, @y = (value >> X_BIT_SHIFT), value & Y_FILTER elsif value.is_a? Coordinates @x, @y = value.x, value.y elsif value.is_a? Array @x, @y = value[0].to_i, value[1].to_i elsif value.is_a? Hash @x, @y = value[:x] || value[:column] || 0, value[:y] || value[:row] || 0 end end |
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x.
14 15 16 |
# File 'lib/spreet/coordinates.rb', line 14 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
14 15 16 |
# File 'lib/spreet/coordinates.rb', line 14 def y @y end |
Instance Method Details
#<=>(other_coordinate) ⇒ Object
57 58 59 |
# File 'lib/spreet/coordinates.rb', line 57 def <=>(other_coordinate) self.to_i <=> other_coordinate.to_i end |
#==(other_coordinate) ⇒ Object
53 54 55 |
# File 'lib/spreet/coordinates.rb', line 53 def ==(other_coordinate) other_coordinate.x == self.x and other_coordinate.y == self.y end |
#to_a ⇒ Object
41 42 43 |
# File 'lib/spreet/coordinates.rb', line 41 def to_a [@x, @y] end |
#to_hash ⇒ Object
45 46 47 |
# File 'lib/spreet/coordinates.rb', line 45 def to_hash {:x=>@x, :y=>@y} end |
#to_i ⇒ Object
49 50 51 |
# File 'lib/spreet/coordinates.rb', line 49 def to_i (@x << X_BIT_SHIFT) + @y end |
#to_s ⇒ Object
37 38 39 |
# File 'lib/spreet/coordinates.rb', line 37 def to_s @x.to_s(26).tr(BASE_26_BEF, BASE_26_AFT).upcase+(@y+1).to_s(10) end |