Class: Geometry::SlopeInterceptLine

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

Instance Attribute Summary

Attributes included from SlopedLine

#horizontal?, #slope, #vertical?

Attributes inherited from Line

#horizontal?, #slope, #vertical?

Instance Method Summary collapse

Methods inherited from Line

[], horizontal, new, vertical

Methods included from ClusterFactory

included

Constructor Details

#initialize(slope, intercept) ⇒ SlopeInterceptLine

Returns a new instance of SlopeInterceptLine.

Parameters:

  • slope (Number)

    the slope

  • intercept (Number)

    the location of the y-axis intercept



177
178
179
180
# File 'lib/geometry/line.rb', line 177

def initialize(slope, intercept)
    @slope = slope
    @intercept = intercept
end

Instance Method Details

#==(other) ⇒ Object

Two Geometry::SlopeInterceptLines are equal if both have equal slope and intercept



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/geometry/line.rb', line 183

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

Returns:

  • (Boolean)


198
199
200
# File 'lib/geometry/line.rb', line 198

def eql?(other)
    (intercept == other.intercept) && (slope == other.slope)
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



205
206
207
208
209
210
211
212
# File 'lib/geometry/line.rb', line 205

def intercept(axis=:y)
    case axis
	when :x
	    vertical? ? @intercept : (horizontal? ? nil : (-@intercept/@slope))
	when :y
	    vertical? ? nil : @intercept
    end
end

#to_sObject



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

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