Class: Sevgi::Geometry::Equation

Inherits:
Object
  • Object
show all
Defined in:
lib/sevgi/geometry/equation.rb,
lib/sevgi/geometry/equation/linear.rb,
lib/sevgi/geometry/equation/quadratic.rb

Overview

Base class for geometric equations that can intersect with each other.

Defined Under Namespace

Classes: Linear, Quadratic

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.diagonal(slope:, intercept:) ⇒ Sevgi::Geometry::Equation::Linear::Diagonal

Builds a non-axis-aligned linear equation.

Parameters:

  • slope (Numeric)

    line slope

  • intercept (Numeric)

    y-intercept

Returns:



11
# File 'lib/sevgi/geometry/equation.rb', line 11

def self.diagonal(slope:, intercept:) = Linear::Diagonal.new(slope:, intercept:)

.horizontal(const) ⇒ Sevgi::Geometry::Equation::Linear::Horizontal

Builds a horizontal linear equation.

Parameters:

  • const (Numeric)

    y coordinate

Returns:



16
# File 'lib/sevgi/geometry/equation.rb', line 16

def self.horizontal(const) = Linear::Horizontal.new(const)

.vertical(const) ⇒ Sevgi::Geometry::Equation::Linear::Vertical

Builds a vertical linear equation.

Parameters:

  • const (Numeric)

    x coordinate

Returns:



21
# File 'lib/sevgi/geometry/equation.rb', line 21

def self.vertical(const) = Linear::Vertical.new(const)

Instance Method Details

#intersect(other) ⇒ Array<Sevgi::Geometry::Point>

Intersects this equation with another equation.

Parameters:

Returns:

Raises:

  • (Sevgi::Geometry::Error)

    when other is not an equation

  • (Sevgi::PanicError)

    when the equation combination is not implemented



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sevgi/geometry/equation.rb', line 28

def intersect(other)
  Error.("Must be an equation: #{other}") unless other.is_a?(Equation)

  points = case [self, other]
  in [Linear, Linear]
    linear_vs_linear(other)
  in [Linear, Quadratic]
    linear_vs_quadratic(other)
  in [Quadratic, Quadratic]
    quadratic_vs_quadratic(other)
  else
    PanicError.("Intersection not implemented: #{self.class} / #{other.class}")
  end

  Array(points)
end

#y(_x) ⇒ Float

This method is abstract.

Subclasses implement equation-specific mapping.

Evaluates y for an x coordinate.

Parameters:

  • _x (Numeric)

    x coordinate

Returns:

  • (Float)

Raises:

  • (Sevgi::PanicError)

    when a subclass does not implement y



50
# File 'lib/sevgi/geometry/equation.rb', line 50

def y(_x, ...) = PanicError.("#{self.class}#y must be implemented")