Module: Budgie::Index

Included in:
DrawPickerMap, Map, Selection
Defined in:
lib/budgie/index.rb

Instance Method Summary collapse

Instance Method Details

#from_index(value) ⇒ Object

Reverses #to



18
19
20
21
22
23
24
# File 'lib/budgie/index.rb', line 18

def from_index(value)
  if value % 2 == 0
    value / 2
  else
    -(1 + value) / 2
  end
end

#to_index(value) ⇒ Object

Encodes negative values as positive ones 0 => 0 1 => 2 2 => 4 -1 => 1 -2 => 3 etc



9
10
11
12
13
14
15
# File 'lib/budgie/index.rb', line 9

def to_index(value)
  if value >= 0
    2 * value
  else
    -(1 + 2 * value)
  end
end