Class: Geometry::TwoPointLine

Inherits:
Line
  • Object
show all
Defined in:
lib/geometry/line.rb

Instance Attribute Summary collapse

Accessors collapse

Instance Method Summary collapse

Methods inherited from Line

[], horizontal, new, vertical

Methods included from ClusterFactory

included

Constructor Details

#initialize(first, last) ⇒ TwoPointLine

Returns a new instance of TwoPointLine.

Parameters:

  • first (Point)

    the starting point

  • last (Point)

    the end point



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

#firstObject

Returns the value of attribute first.



223
224
225
# File 'lib/geometry/line.rb', line 223

def first
  @first
end

#lastObject

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

Returns:

  • (Boolean)


259
260
261
# File 'lib/geometry/line.rb', line 259

def eql?(other)
    (first == other.first) && (last == other.last)
end

#horizontal?Boolean

Returns:

  • (Boolean)


270
271
272
# File 'lib/geometry/line.rb', line 270

def horizontal?
    first.y == last.y
end

#inspectObject 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

Parameters:

  • axis (Symbol) (defaults to: :y)

    the axis to intercept (either :x or :y)

Returns:

  • (Number)

    the location of the 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

#slopeObject

!@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

Returns:

  • (Boolean)


274
275
276
# File 'lib/geometry/line.rb', line 274

def vertical?
    first.x == last.x
end