Class: Sevgi::Geometry::Equation::Linear::Diagonal
- Inherits:
-
Linear
- Object
- Linear
- Sevgi::Geometry::Equation::Linear::Diagonal
- Defined in:
- lib/sevgi/geometry/equation/linear.rb
Overview
Non-axis-aligned linear equation in y = slope * x + intercept form.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#intercept ⇒ Object
readonly
Returns the value of attribute intercept.
-
#slope ⇒ Float
readonly
Line slope.
Instance Method Summary collapse
-
#approx(precision = nil) ⇒ Sevgi::Geometry::Equation::Linear::Diagonal
Returns an equation rounded to precision.
-
#eql?(other) ⇒ Boolean
(also: #==)
Reports strict equation equality.
-
#hash ⇒ Integer
Returns a hash compatible with strict equality.
-
#initialize(slope:, intercept:) ⇒ void
constructor
Creates a diagonal linear equation.
-
#left?(point) ⇒ Boolean
Reports whether a point is on the left side of the line in screen coordinates.
-
#on?(point) ⇒ Boolean
Reports whether a point is on the line.
-
#right?(point) ⇒ Boolean
Reports whether a point is on the right side of the line in screen coordinates.
-
#shift(distance = nil, dx: nil, dy: nil) ⇒ Sevgi::Geometry::Equation::Linear::Diagonal
Returns a parallel equation shifted by a signed perpendicular offset.
-
#to_s ⇒ String
Formats the equation for display.
-
#x(y) ⇒ Float
Evaluates x for a y coordinate.
-
#y(x) ⇒ Float
Evaluates y for an x coordinate.
Constructor Details
#initialize(slope:, intercept:) ⇒ void
Creates a diagonal linear equation.
20 21 22 23 24 25 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 20 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.
14 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 14 attr_reader :slope, :intercept |
#slope ⇒ Float (readonly)
Returns line slope.
14 15 16 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 14 def slope @slope end |
Instance Method Details
#approx(precision = nil) ⇒ Sevgi::Geometry::Equation::Linear::Diagonal
Returns an equation rounded to precision.
30 31 32 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 30 def approx(precision = nil) self.class.new(slope: F.approx(slope, precision), intercept: F.approx(intercept, precision)) end |
#eql?(other) ⇒ Boolean Also known as: ==
Reports strict equation equality.
37 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 37 def eql?(other) = self.class == other.class && [slope, intercept] == [other.slope, other.intercept] |
#hash ⇒ Integer
Returns a hash compatible with strict equality.
41 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 41 def hash = [self.class, slope, intercept].hash |
#left?(point) ⇒ Boolean
Reports whether a point is on the left side of the line in screen coordinates.
47 48 49 50 51 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 47 def left?(point) point = Tuple[Point, point] F.gt?(point.y, y(point.x)) end |
#on?(point) ⇒ Boolean
Reports whether a point is on the line.
57 58 59 60 61 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 57 def on?(point) point = Tuple[Point, point] F.eq?(point.y, y(point.x)) end |
#right?(point) ⇒ Boolean
Reports whether a point is on the right side of the line in screen coordinates.
67 68 69 70 71 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 67 def right?(point) point = Tuple[Point, point] F.lt?(point.y, y(point.x)) end |
#shift(distance = nil, dx: nil, dy: nil) ⇒ Sevgi::Geometry::Equation::Linear::Diagonal
Returns a parallel equation shifted by a signed perpendicular offset.
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 78 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 ⇒ String
Formats the equation for display.
92 93 94 95 96 97 98 99 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 92 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) ⇒ Float
Evaluates x for a y coordinate.
104 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 104 def x(y) = (y - intercept) / slope |
#y(x) ⇒ Float
Evaluates y for an x coordinate.
109 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 109 def y(x) = (slope * x) + intercept |