Class: Sevgi::Geometry::Equation::Quadratic

Inherits:
Equation
  • Object
show all
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.

Examples:

Intersect a unit circle carrier with a horizontal line

equation = Sevgi::Geometry::Equation.quadratic(1, 0, 1, 0, 0, -1)
equation.y(0) # => [-1.0, 1.0]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*coefficients, origin: Origin) ⇒ void

Creates a quadratic equation in local coordinates.

Parameters:

  • coefficients (Array<Numeric>) —

    six finite coefficients, with a nonzero second-degree term

  • origin (Sevgi::Geometry::Point, Array<Numeric>) (defaults to: Origin) —

    local coordinate origin

Raises:



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.

Returns:

  • (Array<Float>) —

    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.

Returns:



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.

Parameters:

  • other (Object) —

    comparison target

Returns:

  • (Boolean)


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.

Returns:

  • (Integer)


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.

Parameters:

  • x (Numeric) —

    finite world x coordinate

Returns:

  • (Array<Float>) —

    zero, one, or two y coordinates

Raises:



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