Class: Picky::Calculations::Location
- 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
-
#anchor ⇒ Object
Returns the value of attribute anchor.
-
#grid ⇒ Object
readonly
Returns the value of attribute grid.
-
#precision ⇒ Object
readonly
Returns the value of attribute precision.
Instance Method Summary collapse
- #add_margin(length) ⇒ Object
- #calculate(location) ⇒ Object
- #calculated_range(location) ⇒ Object
-
#initialize(user_grid, anchor = 0.0, precision = nil) ⇒ Location
constructor
A new instance of Location.
- #range(around_location) ⇒ Object
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
#anchor ⇒ Object
Returns the value of attribute anchor.
14 15 16 |
# File 'lib/picky/calculations/location.rb', line 14 def anchor @anchor end |
#grid ⇒ Object (readonly)
Returns the value of attribute grid.
14 15 16 |
# File 'lib/picky/calculations/location.rb', line 14 def grid @grid end |
#precision ⇒ Object (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 |