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.
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.
-
#on?(point) ⇒ Boolean
Reports whether a point is on the line.
-
#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.
74 75 76 77 78 79 80 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 74 def initialize(slope:, intercept:) super() @slope = Real[:slope, slope] @intercept = Real[:intercept, intercept] Error.("A diagonal equation requires a non-zero slope") if @slope.zero? end |
Instance Attribute Details
#intercept ⇒ Float (readonly)
Returns the y-intercept.
67 68 69 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 67 def intercept @intercept end |
#slope ⇒ Float (readonly)
Returns the line slope.
63 64 65 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 63 def slope @slope end |
Instance Method Details
#approx(precision = nil) ⇒ Sevgi::Geometry::Equation::Linear::Diagonal
Returns an equation rounded to precision.
85 86 87 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 85 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.
92 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 92 def eql?(other) = Nonvertical.equal?(self, other) |
#hash ⇒ Integer
Returns a hash compatible with strict equality.
96 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 96 def hash = Nonvertical.hash(self) |
#on?(point) ⇒ Boolean
Reports whether a point is on the line.
102 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 102 def on?(point) = Nonvertical.on?(self, point) |
#shift(distance = nil, dx: nil, dy: nil) ⇒ Sevgi::Geometry::Equation::Linear::Diagonal
Returns a parallel equation shifted by a signed perpendicular offset. Positive distance moves to screen-left of the equation's canonical increasing-x direction.
111 112 113 114 115 116 117 118 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 111 def shift(distance = nil, dx: nil, dy: nil) distance, dx, dy = shift_values(distance, dx, dy) angle = F.atan(slope) dx += distance * F.sin(angle) dy -= distance * F.cos(angle) Diagonal.new(slope:, intercept: intercept - (slope * dx) + dy) end |
#to_s ⇒ String
Formats the equation for display.
122 123 124 125 126 127 128 129 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 122 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.
135 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 135 def x(y) = (Real[:y, y] - intercept) / slope |
#y(x) ⇒ Float
Evaluates y for an x coordinate.
141 |
# File 'lib/sevgi/geometry/equation/linear.rb', line 141 def y(x) = Nonvertical.y(self, x) |