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(point0, point1) ⇒ TwoPointLine

Returns a new instance of TwoPointLine.



188
189
190
# File 'lib/geometry/line.rb', line 188

def initialize(point0, point1)
    @first, @last = [Point[point0], Point[point1]]
end

Instance Attribute Details

#firstObject (readonly)

Returns the value of attribute first.



186
187
188
# File 'lib/geometry/line.rb', line 186

def first
  @first
end

#lastObject (readonly)

Returns the value of attribute last.



186
187
188
# File 'lib/geometry/line.rb', line 186

def last
  @last
end

Instance Method Details

#==(other) ⇒ Object

Two Geometry::TwoPointLines are equal if both have equal Points in the same order



197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/geometry/line.rb', line 197

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)


214
215
216
# File 'lib/geometry/line.rb', line 214

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

#inspectObject Also known as: to_s



191
192
193
# File 'lib/geometry/line.rb', line 191

def inspect
    'Line(' + @first.inspect + ', ' + @last.inspect + ')'
end

#slopeObject

!@attribute [r[ slope

@return [Number]	the slope of the {Line}


221
222
223
# File 'lib/geometry/line.rb', line 221

def slope
    (last.y - first.y)/(last.x - first.x)
end