Class: Geometry::SlopeInterceptLine

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(slope, intercept) ⇒ SlopeInterceptLine

Returns a new instance of SlopeInterceptLine.

Parameters:

  • slope (Number)

    the slope

  • intercept (Number)

    the location of the y-axis intercept



138
139
140
141
# File 'lib/geometry/line.rb', line 138

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

Instance Attribute Details

#slopeNumber (readonly)

Returns the slope of the Line.

Returns:

  • (Number)

    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

Returns:

  • (Boolean)


159
160
161
# File 'lib/geometry/line.rb', line 159

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

#horizontal?Boolean

Returns:

  • (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_sObject



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

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

#vertical?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/geometry/line.rb', line 166

def vertical?
    (1/0.0) == @slope
end