Class: Geometry::PointSlopeLine

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

Instance Attribute Summary collapse

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(point, slope) ⇒ PointSlopeLine

Returns a new instance of PointSlopeLine.

Parameters:



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

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

Instance Attribute Details

#pointObject

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

Returns:

  • (Boolean)


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

Parameters:

  • axis (Symbol) (defaults to: :y)

    the axis to intercept (either :x or :y)

Returns:

  • (Number)

    the location of the 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_sObject



154
155
156
# File 'lib/geometry/line.rb', line 154

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