Class: Geometry::PointSlopeLine

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Line

[], horizontal, new, vertical

Methods included from ClusterFactory

included

Constructor Details

#initialize(point, slope) ⇒ PointSlopeLine

Returns a new instance of PointSlopeLine.

Parameters:



99
100
101
102
# File 'lib/geometry/line.rb', line 99

def initialize(point, slope)
    @point = Point[point]
    @slope = slope
end

Instance Attribute Details

#pointObject

Returns the value of attribute point.



92
93
94
# File 'lib/geometry/line.rb', line 92

def point
  @point
end

#slopeNumber (readonly)

Returns the slope of the Line.

Returns:

  • (Number)

    the slope of the Line



95
96
97
# File 'lib/geometry/line.rb', line 95

def slope
  @slope
end

Instance Method Details

#==(other) ⇒ Object

Two Geometry::PointSlopeLines are equal if both have equal slope and origin



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/geometry/line.rb', line 105

def ==(other)
    case other
	when SlopeInterceptLine
	    # Check that the slopes are equal and that the starting point will solve the slope-intercept equation
	    (slope == other.slope) && (point.y == other.slope * point.x + other.intercept)
	when TwoPointLine
	    # Plug both of other's endpoints into the line equation and check that they solve it
	    first_diff = other.first - point
	    last_diff = other.last - point
	    (first_diff.y == slope*first_diff.x) && (last_diff.y == slope*last_diff.x)
	else
	    self.eql? other
    end
end

#eql?(other) ⇒ Boolean

Two Geometry::PointSlopeLines are equal if both have equal slopes and origins @note eql? does not check for equivalence between cluster subclases

Returns:

  • (Boolean)


122
123
124
# File 'lib/geometry/line.rb', line 122

def eql?(other)
    (point == other.point) && (slope == other.slope)
end

#to_sObject



126
127
128
# File 'lib/geometry/line.rb', line 126

def to_s
    'Line(' + @slope.to_s + ',' + @point.to_s + ')'
end