Class: Sevgi::Geometry::Equation::Linear::Diagonal

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

Overview

Non-axis-aligned linear equation in y = slope * x + intercept form.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(slope:, intercept:) ⇒ void

Creates a diagonal linear equation.

Parameters:

  • slope (Numeric)

    line slope

  • intercept (Numeric)

    y-intercept

Raises:



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

#interceptFloat (readonly)

Returns the y-intercept.

Returns:

  • (Float)


67
68
69
# File 'lib/sevgi/geometry/equation/linear.rb', line 67

def intercept
  @intercept
end

#slopeFloat (readonly)

Returns the line slope.

Returns:

  • (Float)


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.

Parameters:

  • precision (Integer, nil) (defaults to: nil)

    decimal precision, or nil for the current function default

Returns:



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.

Parameters:

  • other (Object)

    object to compare

Returns:

  • (Boolean)


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

def eql?(other) = Nonvertical.equal?(self, other)

#hashInteger

Returns a hash compatible with strict equality.

Returns:

  • (Integer)


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.

Parameters:

Returns:

  • (Boolean)

Raises:



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.

Parameters:

  • distance (Numeric, nil) (defaults to: nil)

    signed perpendicular offset

  • dx (Numeric, nil) (defaults to: nil)

    explicit x translation

  • dy (Numeric, nil) (defaults to: nil)

    explicit y translation

Returns:

Raises:



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_sString

Formats the equation for display.

Returns:

  • (String)


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.

Parameters:

  • y (Numeric)

    y coordinate

Returns:

  • (Float)

Raises:



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.

Parameters:

  • x (Numeric)

    x coordinate

Returns:

  • (Float)

Raises:



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

def y(x) = Nonvertical.y(self, x)