Class: Geometry::TwoPointLine
Instance Attribute Summary collapse
-
#first ⇒ Object
Returns the value of attribute first.
-
#last ⇒ Object
Returns the value of attribute last.
Accessors collapse
- #horizontal? ⇒ Boolean
-
#intercept(axis = :y) ⇒ Number
Find the requested axis intercept.
-
#slope ⇒ Object
!@attribute [r[ slope @return [Number] the slope of the Line.
- #vertical? ⇒ Boolean
Instance Method Summary collapse
-
#==(other) ⇒ Object
Two TwoPointLines are equal if both have equal Points in the same order.
-
#eql?(other) ⇒ Boolean
Two TwoPointLines are equal if both have equal endpoints @note eql? does not check for equivalence between cluster subclases.
-
#initialize(first, last) ⇒ TwoPointLine
constructor
A new instance of TwoPointLine.
- #inspect ⇒ Object (also: #to_s)
Methods inherited from Line
[], horizontal, new, vertical
Methods included from ClusterFactory
Constructor Details
#initialize(first, last) ⇒ TwoPointLine
Returns a new instance of TwoPointLine.
231 232 233 234 |
# File 'lib/geometry/line.rb', line 231 def initialize(first, last) @first = Point[first] @last = Point[last] end |
Instance Attribute Details
#first ⇒ Object
Returns the value of attribute first.
223 224 225 |
# File 'lib/geometry/line.rb', line 223 def first @first end |
#last ⇒ Object
Returns the value of attribute last.
227 228 229 |
# File 'lib/geometry/line.rb', line 227 def last @last end |
Instance Method Details
#==(other) ⇒ Object
Two Geometry::TwoPointLines are equal if both have equal Points in the same order
242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/geometry/line.rb', line 242 def ==(other) case other when PointSlopeLine # Plug both endpoints into the line equation and check that they solve it first_diff = first - other.point last_diff = last - other.point (first_diff.y == other.slope*first_diff.x) && (last_diff.y == other.slope*last_diff.x) when SlopeInterceptLine # Check that both endpoints solve the line equation ((first.y == other.slope * first.x + other.intercept)) && (last.y == (other.slope * last.x + other.intercept)) else self.eql?(other) || ((first == other.last) && (last == other.first)) end end |
#eql?(other) ⇒ Boolean
Two Geometry::TwoPointLines are equal if both have equal endpoints
@note eql? does not check for equivalence between cluster subclases
259 260 261 |
# File 'lib/geometry/line.rb', line 259 def eql?(other) (first == other.first) && (last == other.last) end |
#horizontal? ⇒ Boolean
270 271 272 |
# File 'lib/geometry/line.rb', line 270 def horizontal? first.y == last.y end |
#inspect ⇒ Object Also known as: to_s
236 237 238 |
# File 'lib/geometry/line.rb', line 236 def inspect 'Line(' + @first.inspect + ', ' + @last.inspect + ')' end |
#intercept(axis = :y) ⇒ Number
Find the requested axis intercept
281 282 283 284 285 286 287 288 |
# File 'lib/geometry/line.rb', line 281 def intercept(axis=:y) case axis when :x vertical? ? first.x : (horizontal? ? nil : (first.x - first.y/slope)) when :y vertical? ? nil : (horizontal? ? first.y : (first.y - slope * first.x)) end end |
#slope ⇒ Object
!@attribute [r[ slope
@return [Number] the slope of the {Line}
266 267 268 |
# File 'lib/geometry/line.rb', line 266 def slope (last.y - first.y)/(last.x - first.x) end |
#vertical? ⇒ Boolean
274 275 276 |
# File 'lib/geometry/line.rb', line 274 def vertical? first.x == last.x end |