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

Abstract base class for geometry equations used in boundary intersections.

The supported public factories build horizontal, vertical, and diagonal linear equations. #intersect always returns an Array: no intersection is [], while one crossing is a one-item Array. Coincident parallel lines do not represent a finite intersection and also return an empty Array.

Examples:

Intersect two linear equations

diagonal = Sevgi::Geometry::Equation.diagonal(slope: 1, intercept: 0)
vertical = Sevgi::Geometry::Equation.vertical(3)
diagonal.intersect(vertical).map(&:deconstruct) # => [[3.0, 3.0]]

See Also:

Defined Under Namespace

Classes: Linear

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:

Raises:



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

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:

Raises:



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

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:

Raises:



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

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sevgi/geometry/equation.rb', line 43

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



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

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