Class: Sevgi::Geometry::Equation::Linear::Diagonal
- Inherits:
-
Linear
- Object
- Linear
- Sevgi::Geometry::Equation::Linear::Diagonal
- Defined in:
- lib/sevgi/geometry/equation/linear.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#intercept ⇒ Object
readonly
Returns the value of attribute intercept.
-
#slope ⇒ Object
readonly
Returns the value of attribute slope.
Instance Method Summary collapse
- #approx(precision = nil) ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
-
#initialize(slope:, intercept:) ⇒ Diagonal
constructor
A new instance of Diagonal.
- #left?(point) ⇒ Boolean
- #on?(point) ⇒ Boolean
- #right?(point) ⇒ Boolean
- #shift(distance = nil, dx: nil, dy: nil) ⇒ Object
- #to_s ⇒ Object
- #x(y) ⇒ Object
- #y(x) ⇒ Object
Constructor Details
#initialize(slope:, intercept:) ⇒ Diagonal
Returns a new instance of Diagonal.
10 11 12 13 14 15 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 10 def initialize(slope:, intercept:) super() @slope = slope.to_f @intercept = intercept.to_f end |
Instance Attribute Details
#intercept ⇒ Object (readonly)
Returns the value of attribute intercept.
8 9 10 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 8 def intercept @intercept end |
#slope ⇒ Object (readonly)
Returns the value of attribute slope.
8 9 10 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 8 def slope @slope end |
Instance Method Details
#approx(precision = nil) ⇒ Object
17 18 19 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 17 def approx(precision = nil) self.class.new(slope: F.approx(slope, precision), intercept: F.approx(intercept, precision)) end |
#eql?(other) ⇒ Boolean Also known as: ==
21 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 21 def eql?(other) = self.class == other.class && [slope, intercept] == [other.slope, other.intercept] |
#hash ⇒ Object
23 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 23 def hash = [self.class, slope, intercept].hash |
#left?(point) ⇒ Boolean
25 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 25 def left?(point) = F.gt?(point.y, y(point.x)) |
#on?(point) ⇒ Boolean
27 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 27 def on?(point) = F.eq?(point.y, y(point.x)) |
#right?(point) ⇒ Boolean
29 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 29 def right?(point) = F.lt?(point.y, y(point.x)) |
#shift(distance = nil, dx: nil, dy: nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 31 def shift(distance = nil, dx: nil, dy: nil) dx ||= 0.0 dy ||= 0.0 if distance dx += distance * F.sin(angle = F.atan(slope)) dy -= distance * F.cos(angle) end Diagonal.new(slope:, intercept: intercept - (slope * dx) + dy) end |
#to_s ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 43 def to_s strings = [] strings << "#{F.approx(slope)} * x" unless F.zero?(slope) strings << F.approx(intercept).abs.to_s unless F.zero?(intercept) "Linear<y = #{strings.join(intercept.positive? ? " + " : " - ")}>" end |
#x(y) ⇒ Object
52 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 52 def x(y) = (y - intercept) / slope |
#y(x) ⇒ Object
54 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 54 def y(x) = (slope * x) + intercept |