Class: Dieses::Geometry::Equation::Slant
- Inherits:
-
Object
- Object
- Dieses::Geometry::Equation::Slant
- Defined in:
- lib/dieses/geometry/equation/slant.rb
Instance Attribute Summary collapse
-
#intercept ⇒ Object
readonly
Returns the value of attribute intercept.
-
#slope ⇒ Object
readonly
Returns the value of attribute slope.
Instance Method Summary collapse
- #angle_in_radian ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
-
#initialize(slope:, intercept:) ⇒ Slant
constructor
A new instance of Slant.
- #intersect(other) ⇒ Object
- #left?(point, precision: Support::Float.precision) ⇒ Boolean
- #onto?(point, precision: Support::Float.precision) ⇒ Boolean
- #right?(point, precision: Support::Float.precision) ⇒ Boolean
- #to_h ⇒ Object
- #to_s ⇒ Object
-
#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).
- #x(y) ⇒ Object
- #y(x) ⇒ Object
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
#intercept ⇒ Object (readonly)
Returns the value of attribute intercept.
7 8 9 |
# File 'lib/dieses/geometry/equation/slant.rb', line 7 def intercept @intercept end |
#slope ⇒ Object (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_radian ⇒ Object
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: ==
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 |
#hash ⇒ Object
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
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
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
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_h ⇒ Object
78 79 80 |
# File 'lib/dieses/geometry/equation/slant.rb', line 78 def to_h { slope: scope, intercept: intercept } end |
#to_s ⇒ Object
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 |