Class: Vedeu::Geometry::GenericCoordinate
- Inherits:
-
Object
- Object
- Vedeu::Geometry::GenericCoordinate
- Defined in:
- lib/vedeu/geometry/generic_coordinate.rb
Overview
Crudely corrects out of range values.
Instance Attribute Summary collapse
- #name ⇒ String readonly protected
- #offset ⇒ Fixnum readonly protected
- #type ⇒ Symbol readonly protected
Instance Method Summary collapse
-
#bd ⇒ Fixnum
private
Return the :bx or :by value from the border.
-
#bdn ⇒ Fixnum
private
Return the :bxn or :byn value from the border.
- #border ⇒ Object private
-
#coordinate_type ⇒ Fixnum
private
Ascertain the correct methods to use for determining the coordinates.
-
#d ⇒ Fixnum
private
Return the :x or :y value from the border.
-
#d_dn ⇒ Fixnum
private
Return the :width or :height value from the border.
-
#d_range ⇒ Array
private
Returns an array with all coordinates from d to dn.
-
#defaults ⇒ Hash
private
The default values for a new instance of this class.
-
#dn ⇒ Fixnum
(also: #xn, #yn)
Returns the maximum coordinate for an area.
-
#dn_index ⇒ Fixnum
private
Returns the maximum index for an area.
-
#initialize(attributes = {}) ⇒ Vedeu::Geometry::GenericCoordinate
constructor
Return a new instance of Vedeu::Geometry::GenericCoordinate.
-
#position ⇒ Fixnum
(also: #x_position, #y_position)
Returns the coordinate for a given index.
Constructor Details
#initialize(attributes = {}) ⇒ Vedeu::Geometry::GenericCoordinate
Return a new instance of Vedeu::Geometry::GenericCoordinate.
16 17 18 19 20 21 22 |
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 16 def initialize(attributes = {}) @attributes = defaults.merge!(attributes) @attributes.each do |key, value| instance_variable_set("@#{key}", value) end end |
Instance Attribute Details
#name ⇒ String (readonly, protected)
73 74 75 |
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 73 def name @name end |
#offset ⇒ Fixnum (readonly, protected)
77 78 79 |
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 77 def offset @offset end |
#type ⇒ Symbol (readonly, protected)
81 82 83 |
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 81 def type @type end |
Instance Method Details
#bd ⇒ Fixnum (private)
Return the :bx or :by value from the border.
100 101 102 |
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 100 def bd border.send(coordinate_type[1]) end |
#bdn ⇒ Fixnum (private)
Return the :bxn or :byn value from the border.
107 108 109 |
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 107 def bdn border.send(coordinate_type[2]) end |
#border ⇒ Object (private)
86 87 88 |
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 86 def border @border ||= Vedeu.borders.by_name(name) end |
#coordinate_type ⇒ Fixnum (private)
Ascertain the correct methods to use for determining the coordinates.
123 124 125 126 127 128 129 130 131 |
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 123 def coordinate_type @_type ||= case type when :x then [:x, :bx, :bxn, :width] when :y then [:y, :by, :byn, :height] else fail Vedeu::Error::InvalidSyntax, 'Coordinate type not given, cannot continue.' end end |
#d ⇒ Fixnum (private)
Return the :x or :y value from the border.
93 94 95 |
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 93 def d border.send(coordinate_type[0]) end |
#d_dn ⇒ Fixnum (private)
Return the :width or :height value from the border.
114 115 116 |
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 114 def d_dn border.send(coordinate_type[3]) end |
#d_range ⇒ Array (private)
Returns an array with all coordinates from d to dn.
159 160 161 |
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 159 def d_range (d...dn).to_a end |
#defaults ⇒ Hash (private)
The default values for a new instance of this class.
166 167 168 169 170 171 172 |
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 166 def defaults { name: '', offset: nil, type: :x, } end |
#dn ⇒ Fixnum Also known as: xn, yn
Returns the maximum coordinate for an area.
32 33 34 35 36 37 38 39 40 |
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 32 def dn if d_dn <= 0 0 else d + d_dn end end |
#dn_index ⇒ Fixnum (private)
Returns the maximum index for an area.
140 141 142 143 144 145 146 147 148 |
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 140 def dn_index if d_dn < 1 0 else d_dn - 1 end end |
#position ⇒ Fixnum Also known as: x_position, y_position
Returns the coordinate for a given index.
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 54 def position pos = case when offset <= 0 then d when offset > dn_index then dn else d_range[offset] end pos = pos < bd ? bd : pos pos = pos > bdn ? bdn : pos pos end |