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

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

Direct Known Subclasses

Horizontal

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(slope:, intercept:) ⇒ Diagonal

Returns a new instance of Diagonal.



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

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.



8
9
10
# File 'lib/sevgi/geometry/equation/linear.rb', line 8

def intercept
  @intercept
end

#slopeObject (readonly)

Returns the value of attribute slope.



8
9
10
# File 'lib/sevgi/geometry/equation/linear.rb', line 8

def slope
  @slope
end

Instance Method Details

#approx(precision = nil) ⇒ Object



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

def approx(precision = nil)
  self.class.new(slope: F.approx(slope, precision), intercept: F.approx(intercept, precision))
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


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

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

#hashObject



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

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

#left?(point) ⇒ Boolean

Returns:

  • (Boolean)


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

def left?(point) = F.gt?(point.y, y(point.x))

#on?(point) ⇒ Boolean

Returns:

  • (Boolean)


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

def on?(point) = F.eq?(point.y, y(point.x))

#right?(point) ⇒ Boolean

Returns:

  • (Boolean)


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

def right?(point) = F.lt?(point.y, y(point.x))

#shift(distance = nil, dx: nil, dy: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sevgi/geometry/equation/linear.rb', line 31

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_sObject



43
44
45
46
47
48
49
50
# File 'lib/sevgi/geometry/equation/linear.rb', line 43

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) ⇒ Object



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

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

#y(x) ⇒ Object



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

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