Class: Geometry::SlopeInterceptLine
Instance Attribute Summary collapse
-
#slope ⇒ Number
readonly
The slope of the Line.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Two SlopeInterceptLines are equal if both have equal slope and intercept.
-
#eql?(other) ⇒ Boolean
Two SlopeInterceptLines are equal if both have equal slopes and intercepts @note eql? does not check for equivalence between cluster subclases.
- #horizontal? ⇒ Boolean
-
#initialize(slope, intercept) ⇒ SlopeInterceptLine
constructor
A new instance of SlopeInterceptLine.
- #intercept(axis = :y) ⇒ Object
- #to_s ⇒ Object
- #vertical? ⇒ Boolean
Methods inherited from Line
[], horizontal, new, vertical
Methods included from ClusterFactory
Constructor Details
#initialize(slope, intercept) ⇒ SlopeInterceptLine
Returns a new instance of SlopeInterceptLine.
138 139 140 141 |
# File 'lib/geometry/line.rb', line 138 def initialize(slope, intercept) @slope = slope @intercept = intercept end |
Instance Attribute Details
#slope ⇒ Number (readonly)
Returns the slope of the Line.
134 135 136 |
# File 'lib/geometry/line.rb', line 134 def slope @slope end |
Instance Method Details
#==(other) ⇒ Object
Two Geometry::SlopeInterceptLines are equal if both have equal slope and intercept
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/geometry/line.rb', line 144 def ==(other) case other when PointSlopeLine # Check that the slopes are equal and that the starting point will solve the slope-intercept equation (slope == other.slope) && (other.point.y == slope * other.point.x + intercept) when TwoPointLine # Check that both endpoints solve the line equation ((other.first.y == slope * other.first.x + intercept)) && (other.last.y == (slope * other.last.x + intercept)) else self.eql? other end end |
#eql?(other) ⇒ Boolean
Two Geometry::SlopeInterceptLines are equal if both have equal slopes and intercepts
@note eql? does not check for equivalence between cluster subclases
159 160 161 |
# File 'lib/geometry/line.rb', line 159 def eql?(other) (intercept == other.intercept) && (slope == other.slope) end |
#horizontal? ⇒ Boolean
163 164 165 |
# File 'lib/geometry/line.rb', line 163 def horizontal? 0 == @slope end |
#intercept(axis = :y) ⇒ Object
170 171 172 173 174 175 176 177 |
# File 'lib/geometry/line.rb', line 170 def intercept(axis=:y) case axis when :x vertical? ? @intercept : (horizontal? ? nil : (-@intercept/@slope)) when :y vertical? ? nil : @intercept end end |
#to_s ⇒ Object
179 180 181 |
# File 'lib/geometry/line.rb', line 179 def to_s 'Line(' + @slope.to_s + ',' + @intercept.to_s + ')' end |
#vertical? ⇒ Boolean
166 167 168 |
# File 'lib/geometry/line.rb', line 166 def vertical? (1/0.0) == @slope end |