Class: Sevgi::Geometry::Equation::Quadratic
- Inherits:
-
Equation
- Object
- Equation
- Sevgi::Geometry::Equation::Quadratic
- Defined in:
- lib/sevgi/geometry/equation/quadratic.rb
Overview
Implicit second-degree carrier with an optional local coordinate origin. The origin avoids expanding large ellipse centers into coefficients that lose the radius through cancellation.
Instance Attribute Summary collapse
-
#coefficients ⇒ Array<Float>
readonly
Immutable coefficients in a, b, c, d, e, f order.
-
#origin ⇒ Sevgi::Geometry::Point
readonly
Local coordinate origin.
Instance Method Summary collapse
-
#eql?(other) ⇒ Boolean
(also: #==)
Reports exact equality of coefficients and the local origin.
-
#hash ⇒ Integer
Returns a hash compatible with strict equality.
-
#initialize(*coefficients, origin: Origin) ⇒ void
constructor
Creates a quadratic equation in local coordinates.
-
#y(x) ⇒ Array<Float>
Returns the finite y roots at a world x coordinate, in ascending order.
Constructor Details
#initialize(*coefficients, origin: Origin) ⇒ void
Creates a quadratic equation in local coordinates.
24 25 26 27 28 29 30 |
# File 'lib/sevgi/geometry/equation/quadratic.rb', line 24 def initialize(*coefficients, origin: Origin) super() Error.("Quadratic equation requires six coefficients") unless coefficients.size == 6 @coefficients = coefficients.each_with_index.map { |value, i| Real[("a".."f").to_a[i], value] }.freeze Error.("Quadratic equation requires a second-degree term") if @coefficients.first(3).all?(&:zero?) @origin = Tuple[Point, origin] end |
Instance Attribute Details
#coefficients ⇒ Array<Float> (readonly)
Returns immutable coefficients in a, b, c, d, e, f order.
15 16 17 |
# File 'lib/sevgi/geometry/equation/quadratic.rb', line 15 def coefficients @coefficients end |
#origin ⇒ Sevgi::Geometry::Point (readonly)
Returns local coordinate origin.
17 18 19 |
# File 'lib/sevgi/geometry/equation/quadratic.rb', line 17 def origin @origin end |
Instance Method Details
#eql?(other) ⇒ Boolean Also known as: ==
Reports exact equality of coefficients and the local origin.
35 |
# File 'lib/sevgi/geometry/equation/quadratic.rb', line 35 def eql?(other) = other.instance_of?(self.class) && coefficients == other.coefficients && origin == other.origin |
#hash ⇒ Integer
Returns a hash compatible with strict equality.
39 |
# File 'lib/sevgi/geometry/equation/quadratic.rb', line 39 def hash = [self.class, coefficients, origin].hash |
#y(x) ⇒ Array<Float>
Returns the finite y roots at a world x coordinate, in ascending order.
47 48 49 50 51 52 53 |
# File 'lib/sevgi/geometry/equation/quadratic.rb', line 47 def y(x) x = Real[:x, x] - origin.x a, b, c, d, e, f = coefficients roots = roots(c, sum(b * x, e), sum(a * x * x, d * x, f)) Error.("y is indeterminate for this quadratic equation") unless roots roots.map { Real[:y, it + origin.y] } end |