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.

Direct Known Subclasses

Horizontal

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



20
21
22
23
24
25
# File 'lib/sevgi/geometry/equation/linear.rb', line 20

def initialize(slope:, intercept:)
  super()

  @slope = slope.to_f
  @intercept = intercept.to_f
end

Instance Attribute Details

#interceptObject (readonly)

Returns the value of attribute intercept.



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

attr_reader :slope, :intercept

#slopeFloat (readonly)

Returns line slope.

Returns:

  • (Float)

    line slope



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

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:



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

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)


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

def eql?(other) = self.class == other.class && [slope, intercept] == [other.slope, other.intercept]

#hashInteger

Returns a hash compatible with strict equality.

Returns:

  • (Integer)


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

def hash = [self.class, slope, intercept].hash

#left?(point) ⇒ Boolean

Reports whether a point is on the left side of the line in screen coordinates.

Parameters:

Returns:

  • (Boolean)

Raises:



47
48
49
50
51
# File 'lib/sevgi/geometry/equation/linear.rb', line 47

def left?(point)
  point = Tuple[Point, point]

  F.gt?(point.y, y(point.x))
end

#on?(point) ⇒ Boolean

Reports whether a point is on the line.

Parameters:

Returns:

  • (Boolean)

Raises:



57
58
59
60
61
# File 'lib/sevgi/geometry/equation/linear.rb', line 57

def on?(point)
  point = Tuple[Point, point]

  F.eq?(point.y, y(point.x))
end

#right?(point) ⇒ Boolean

Reports whether a point is on the right side of the line in screen coordinates.

Parameters:

Returns:

  • (Boolean)

Raises:



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

def right?(point)
  point = Tuple[Point, point]

  F.lt?(point.y, y(point.x))
end

#shift(distance = nil, dx: nil, dy: nil) ⇒ Sevgi::Geometry::Equation::Linear::Diagonal

Returns a parallel equation shifted by a signed perpendicular offset.

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:



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sevgi/geometry/equation/linear.rb', line 78

def shift(distance = nil, dx: nil, dy: nil)
  dx ||= 0.0
  dy ||= 0.0

  if distance
    dx += distance * F.sin(angle = F.atan(slope))
    dy -= distance * F.cos(angle)
  end

  Diagonal.new(slope:, intercept: intercept - (slope * dx) + dy)
end

#to_sString

Formats the equation for display.

Returns:

  • (String)


92
93
94
95
96
97
98
99
# File 'lib/sevgi/geometry/equation/linear.rb', line 92

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)


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

def x(y) = (y - intercept) / slope

#y(x) ⇒ Float

Evaluates y for an x coordinate.

Parameters:

  • x (Numeric)

    x coordinate

Returns:

  • (Float)


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

def y(x) = (slope * x) + intercept