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.
21 22 23 24 25 26 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 21 def initialize(slope:, intercept:) super() @slope = Real[:slope, slope] @intercept = Real[:intercept, intercept] 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.
31 32 33 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 31 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.
38 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 38 def eql?(other) = self.class == other.class && [slope, intercept] == [other.slope, other.intercept] |
#hash ⇒ Integer
Returns a hash compatible with strict equality.
42 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 42 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.
48 49 50 51 52 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 48 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.
58 59 60 61 62 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 58 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.
68 69 70 71 72 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 68 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.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 79 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.
93 94 95 96 97 98 99 100 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 93 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.
105 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 105 def x(y) = (y - intercept) / slope |
#y(x) ⇒ Float
Evaluates y for an x coordinate.
110 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 110 def y(x) = (slope * x) + intercept |