Class: CTioga::Utils::Boundaries
- Inherits:
-
Object
- Object
- CTioga::Utils::Boundaries
- Defined in:
- lib/CTioga/boundaries.rb
Overview
A small class to handle boundaries
Instance Attribute Summary collapse
-
#bottom ⇒ Object
Returns the value of attribute bottom.
-
#left ⇒ Object
Returns the value of attribute left.
-
#right ⇒ Object
Returns the value of attribute right.
-
#top ⇒ Object
Returns the value of attribute top.
Instance Method Summary collapse
-
#initialize(*args) ⇒ Boundaries
constructor
A new instance of Boundaries.
-
#real_bounds ⇒ Object
Returns [xmin, xmax, ymin, ymax].
- #to_a ⇒ Object
-
#to_hash ⇒ Object
Returns a ‘left’ => …, ‘right’ => .…
-
#where_x?(x) ⇒ Boolean
The position of the given X coordinate with respect to the boundaries.
-
#where_y?(y) ⇒ Boolean
The position of the given Y coordinate with respect to the boundaries.
-
#x_inside?(x) ⇒ Boolean
Returns true if the given X coordinate is within the bounds.
- #xmax ⇒ Object
- #xmin ⇒ Object
-
#y_inside?(y) ⇒ Boolean
Returns true if the given Y coordinate is within the bounds.
- #ymax ⇒ Object
- #ymin ⇒ Object
Constructor Details
#initialize(*args) ⇒ Boundaries
Returns a new instance of Boundaries.
66 67 68 69 70 71 72 73 |
# File 'lib/CTioga/boundaries.rb', line 66 def initialize(*args) args.flatten! @left = args[0] @right = args[1] @top = args[2] @bottom = args[3] end |
Instance Attribute Details
#bottom ⇒ Object
Returns the value of attribute bottom.
64 65 66 |
# File 'lib/CTioga/boundaries.rb', line 64 def bottom @bottom end |
#left ⇒ Object
Returns the value of attribute left.
64 65 66 |
# File 'lib/CTioga/boundaries.rb', line 64 def left @left end |
#right ⇒ Object
Returns the value of attribute right.
64 65 66 |
# File 'lib/CTioga/boundaries.rb', line 64 def right @right end |
#top ⇒ Object
Returns the value of attribute top.
64 65 66 |
# File 'lib/CTioga/boundaries.rb', line 64 def top @top end |
Instance Method Details
#real_bounds ⇒ Object
Returns [xmin, xmax, ymin, ymax]. Useful, for instance, for Function#bound_values
109 110 111 |
# File 'lib/CTioga/boundaries.rb', line 109 def real_bounds return [xmin, xmax, ymin, ymax] end |
#to_a ⇒ Object
113 114 115 |
# File 'lib/CTioga/boundaries.rb', line 113 def to_a return [@left, @right, @top, @bottom] end |
#to_hash ⇒ Object
Returns a ‘left’ => …, ‘right’ => .… hash containg the boundaries.
119 120 121 122 123 124 125 126 |
# File 'lib/CTioga/boundaries.rb', line 119 def to_hash return { 'left' => @left, 'right' => @right, 'top' => @top, 'bottom' => @bottom } end |
#where_x?(x) ⇒ Boolean
The position of the given X coordinate with respect to the boundaries. Returns either :inside, :left or :right
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/CTioga/boundaries.rb', line 140 def where_x?(x) if x_inside?(x) return :inside elsif @left <= @right if x < @left return :left else return :right end else # Right-to-left order if x < @right return :right else return :left end end end |
#where_y?(y) ⇒ Boolean
The position of the given Y coordinate with respect to the boundaries. Returns either :inside, :top or :bottom
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/CTioga/boundaries.rb', line 160 def where_y?(y) if y_inside?(y) return :inside elsif @top <= @bottom if y < @top return :top else return :bottom end else if y < @top return :bottom else return :top end end end |
#x_inside?(x) ⇒ Boolean
Returns true if the given X coordinate is within the bounds
129 130 131 |
# File 'lib/CTioga/boundaries.rb', line 129 def x_inside?(x) return (x >= xmin && x <= xmax) end |
#xmax ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/CTioga/boundaries.rb', line 83 def xmax if @left <= @right return @right else return @left end end |
#xmin ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/CTioga/boundaries.rb', line 75 def xmin if @left <= @right return @left else return @right end end |
#y_inside?(y) ⇒ Boolean
Returns true if the given Y coordinate is within the bounds
134 135 136 |
# File 'lib/CTioga/boundaries.rb', line 134 def y_inside?(y) return (y >= ymin && y <= ymax) end |
#ymax ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/CTioga/boundaries.rb', line 99 def ymax if @top <= @bottom return @bottom else return @top end end |
#ymin ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/CTioga/boundaries.rb', line 91 def ymin if @top <= @bottom return @top else return @bottom end end |