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

Raises:



23
24
25
26
27
28
# File 'lib/sevgi/geometry/equation/linear.rb', line 23

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

  @slope = Real[:slope, slope]
  @intercept = Real[:intercept, intercept]
end

Instance Attribute Details

#interceptFloat (readonly)

Returns the y-intercept.

Returns:

  • (Float)


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

def intercept
  @intercept
end

#slopeFloat (readonly)

Returns the line slope.

Returns:

  • (Float)


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

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:



33
34
35
# File 'lib/sevgi/geometry/equation/linear.rb', line 33

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)


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

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

#hashInteger

Returns a hash compatible with strict equality.

Returns:

  • (Integer)


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

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:



50
51
52
53
54
# File 'lib/sevgi/geometry/equation/linear.rb', line 50

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:



60
61
62
63
64
# File 'lib/sevgi/geometry/equation/linear.rb', line 60

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:



70
71
72
73
74
# File 'lib/sevgi/geometry/equation/linear.rb', line 70

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:



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sevgi/geometry/equation/linear.rb', line 81

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)


95
96
97
98
99
100
101
102
# File 'lib/sevgi/geometry/equation/linear.rb', line 95

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)


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

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

#y(x) ⇒ Float

Evaluates y for an x coordinate.

Parameters:

  • x (Numeric)

    x coordinate

Returns:

  • (Float)


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

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