Class: Picky::Calculations::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/picky/calculations/location.rb

Overview

A location calculation recalculates a 1-d location to the Picky internal 1-d “grid”.

For example, if you have a location x == 12.3456, it will be recalculated into 3, if the minimum is 9 and the gridlength is 1.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_grid, anchor = 0.0, precision = nil) ⇒ Location

Returns a new instance of Location.



18
19
20
21
22
23
24
# File 'lib/picky/calculations/location.rb', line 18

def initialize user_grid, anchor = 0.0, precision = nil
  @user_grid  = user_grid
  @precision  = precision || 1
  @grid       = @user_grid / (@precision + 0.5)

  self.anchor = anchor
end

Instance Attribute Details

#anchorObject

Returns the value of attribute anchor.



14
15
16
# File 'lib/picky/calculations/location.rb', line 14

def anchor
  @anchor
end

#gridObject (readonly)

Returns the value of attribute grid.



14
15
16
# File 'lib/picky/calculations/location.rb', line 14

def grid
  @grid
end

#precisionObject (readonly)

Returns the value of attribute precision.



14
15
16
# File 'lib/picky/calculations/location.rb', line 14

def precision
  @precision
end

Instance Method Details

#add_margin(length) ⇒ Object



41
42
43
# File 'lib/picky/calculations/location.rb', line 41

def add_margin length
  @anchor -= length
end

#calculate(location) ⇒ Object



57
58
59
# File 'lib/picky/calculations/location.rb', line 57

def calculate location
  ((location - @anchor) / @grid).floor
end

#calculated_range(location) ⇒ Object



47
48
49
# File 'lib/picky/calculations/location.rb', line 47

def calculated_range location
  range calculate(location)
end

#range(around_location) ⇒ Object



52
53
54
# File 'lib/picky/calculations/location.rb', line 52

def range around_location
  (around_location - @precision)..(around_location + @precision)
end