Class: Geometry::PointSlopeLine
- Includes:
- SlopedLine
- Defined in:
- lib/geometry/line.rb
Instance Attribute Summary collapse
-
#point ⇒ Object
Returns the value of attribute point.
Attributes included from SlopedLine
#horizontal?, #slope, #vertical?
Attributes inherited from Line
#horizontal?, #slope, #vertical?
Instance Method Summary collapse
-
#==(other) ⇒ Object
Two PointSlopeLines are equal if both have equal slope and origin.
-
#eql?(other) ⇒ Boolean
Two PointSlopeLines are equal if both have equal slopes and origins @note eql? does not check for equivalence between cluster subclases.
-
#initialize(point, slope) ⇒ PointSlopeLine
constructor
A new instance of PointSlopeLine.
-
#intercept(axis = :y) ⇒ Number
Find the requested axis intercept.
- #to_s ⇒ Object
Methods inherited from Line
[], horizontal, new, vertical
Methods included from ClusterFactory
Constructor Details
#initialize(point, slope) ⇒ PointSlopeLine
Returns a new instance of PointSlopeLine.
127 128 129 130 |
# File 'lib/geometry/line.rb', line 127 def initialize(point, slope) @point = Point[point] @slope = slope end |
Instance Attribute Details
#point ⇒ Object
Returns the value of attribute point.
123 124 125 |
# File 'lib/geometry/line.rb', line 123 def point @point end |
Instance Method Details
#==(other) ⇒ Object
Two Geometry::PointSlopeLines are equal if both have equal slope and origin
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/geometry/line.rb', line 133 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
150 151 152 |
# File 'lib/geometry/line.rb', line 150 def eql?(other) (point == other.point) && (slope == other.slope) end |
#intercept(axis = :y) ⇒ Number
Find the requested axis intercept
161 162 163 164 165 166 167 168 |
# File 'lib/geometry/line.rb', line 161 def intercept(axis=:y) case axis when :x vertical? ? point.x : (horizontal? ? nil : (slope * point.x - point.y)) when :y vertical? ? nil : (horizontal? ? point.y : (point.y - slope * point.x)) end end |
#to_s ⇒ Object
154 155 156 |
# File 'lib/geometry/line.rb', line 154 def to_s 'Line(' + @slope.to_s + ',' + @point.to_s + ')' end |