Class: Dieses::Geometry::Equation::Slant

Inherits:
Object
  • Object
show all
Defined in:
lib/dieses/geometry/equation/slant.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(slope:, intercept:) ⇒ Slant

Returns a new instance of Slant.



9
10
11
12
# File 'lib/dieses/geometry/equation/slant.rb', line 9

def initialize(slope:, intercept:)
  @slope, @intercept = slope.to_f, intercept.to_f
  freeze
end

Instance Attribute Details

#interceptObject (readonly)

Returns the value of attribute intercept.



7
8
9
# File 'lib/dieses/geometry/equation/slant.rb', line 7

def intercept
  @intercept
end

#slopeObject (readonly)

Returns the value of attribute slope.



7
8
9
# File 'lib/dieses/geometry/equation/slant.rb', line 7

def slope
  @slope
end

Instance Method Details

#angle_in_radianObject



39
40
41
# File 'lib/dieses/geometry/equation/slant.rb', line 39

def angle_in_radian
  Math.atan(slope)
end

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

Returns:

  • (Boolean)


68
69
70
# File 'lib/dieses/geometry/equation/slant.rb', line 68

def eql?(other)
  self.class == other.class && to_h == other.to_h
end

#hashObject



74
75
76
# File 'lib/dieses/geometry/equation/slant.rb', line 74

def hash
  self.class.hash ^ to_h.hash
end

#intersect(other) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dieses/geometry/equation/slant.rb', line 43

def intersect(other)
  case other
  when Slant
    x = (other.intercept - intercept) / (slope - other.slope)
    y = slope * x + intercept
  when Steep
    x = other.x
    y = y(x)
  end

  Point.new(x, y)
end

#left?(point, precision: Support::Float.precision) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/dieses/geometry/equation/slant.rb', line 56

def left?(point, precision: Support::Float.precision)
  Support.almost_greater_than(point.y, y(point.x), precision: precision)
end

#onto?(point, precision: Support::Float.precision) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/dieses/geometry/equation/slant.rb', line 64

def onto?(point, precision: Support::Float.precision)
  Support.almost_equal(point.y, y(point.x), precision: precision)
end

#right?(point, precision: Support::Float.precision) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/dieses/geometry/equation/slant.rb', line 60

def right?(point, precision: Support::Float.precision)
  Support.almost_less_than(point.y, y(point.x), precision: precision)
end

#to_hObject



78
79
80
# File 'lib/dieses/geometry/equation/slant.rb', line 78

def to_h
  { slope: scope, intercept: intercept }
end

#to_sObject



82
83
84
# File 'lib/dieses/geometry/equation/slant.rb', line 82

def to_s
  "E(y = #{slope} * x + #{intercept})"
end

#translate(distance = nil, x: nil, y: nil) ⇒ Object

When distance given, y = m * x + n (m, n positive) equation moves to the right (x increases, y decreases)



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dieses/geometry/equation/slant.rb', line 23

def translate(distance = nil, x: nil, y: nil)
  dx, dy = 0, 0

  intercept = self.intercept

  dx, dy = distance * Math.cos(angle_in_radian), -distance * Math.sin(angle_in_radian) if distance

  dx += x if x
  dy += y if y

  intercept -= slope * dx
  intercept += dy

  self.class.new slope: slope, intercept: intercept
end

#x(y) ⇒ Object



14
15
16
# File 'lib/dieses/geometry/equation/slant.rb', line 14

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

#y(x) ⇒ Object



18
19
20
# File 'lib/dieses/geometry/equation/slant.rb', line 18

def y(x)
  slope * x + intercept
end