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 ⇒ Float
readonly
Returns the y-intercept.
-
#slope ⇒ Float
readonly
Returns the 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.
23 24 25 26 27 28 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 23 def initialize(slope:, intercept:) super() @slope = Real[:slope, slope] @intercept = Real[:intercept, intercept] end |
Instance Attribute Details
#intercept ⇒ Float (readonly)
Returns the y-intercept.
16 17 18 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 16 def intercept @intercept end |
#slope ⇒ Float (readonly)
Returns the line slope.
12 13 14 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 12 def slope @slope end |
Instance Method Details
#approx(precision = nil) ⇒ Sevgi::Geometry::Equation::Linear::Diagonal
Returns an equation rounded to precision.
33 34 35 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 33 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.
40 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 40 def eql?(other) = self.class == other.class && [slope, intercept] == [other.slope, other.intercept] |
#hash ⇒ Integer
Returns a hash compatible with strict equality.
44 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 44 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.
50 51 52 53 54 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 50 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.
60 61 62 63 64 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 60 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.
70 71 72 73 74 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 70 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.
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 81 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.
95 96 97 98 99 100 101 102 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 95 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.
107 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 107 def x(y) = (y - intercept) / slope |
#y(x) ⇒ Float
Evaluates y for an x coordinate.
112 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 112 def y(x) = (slope * x) + intercept |