Class: Sevgi::Geometry::Equation::Line::Diagonal
- Inherits:
-
Object
- Object
- Sevgi::Geometry::Equation::Line::Diagonal
- Defined in:
- lib/sevgi/geometry/equation/line/diagonal.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#intercept ⇒ Object
readonly
Returns the value of attribute intercept.
-
#slope ⇒ Object
readonly
Returns the value of attribute slope.
Instance Method Summary collapse
- #approx(precision = nil) ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
-
#initialize(slope:, intercept:) ⇒ Diagonal
constructor
A new instance of Diagonal.
- #intersection(other) ⇒ Object
- #left?(point) ⇒ Boolean
- #onto?(point) ⇒ Boolean
- #right?(point) ⇒ Boolean
- #shift(distance = nil, dx: nil, dy: nil) ⇒ Object
- #to_s ⇒ Object
- #x(y) ⇒ Object
- #y(x) ⇒ Object
Constructor Details
#initialize(slope:, intercept:) ⇒ Diagonal
10 11 12 13 14 |
# File 'lib/sevgi/geometry/equation/line/diagonal.rb', line 10 def initialize(slope:, intercept:) @slope = slope.to_f @intercept = intercept.to_f @direction = F.angles(@slope) end |
Instance Attribute Details
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
8 9 10 |
# File 'lib/sevgi/geometry/equation/line/diagonal.rb', line 8 def direction @direction end |
#intercept ⇒ Object (readonly)
Returns the value of attribute intercept.
8 9 10 |
# File 'lib/sevgi/geometry/equation/line/diagonal.rb', line 8 def intercept @intercept end |
#slope ⇒ Object (readonly)
Returns the value of attribute slope.
8 9 10 |
# File 'lib/sevgi/geometry/equation/line/diagonal.rb', line 8 def slope @slope end |
Instance Method Details
#approx(precision = nil) ⇒ Object
16 17 18 |
# File 'lib/sevgi/geometry/equation/line/diagonal.rb', line 16 def approx(precision = nil) self.class.new(slope: F.approx(slope, precision), intercept: F.approx(intercept, precision)) end |
#eql?(other) ⇒ Boolean Also known as: ==
20 21 22 |
# File 'lib/sevgi/geometry/equation/line/diagonal.rb', line 20 def eql?(other) self.class == other.class && [ slope, intercept ] == [ other.slope, other.intercept ] end |
#hash ⇒ Object
24 25 26 |
# File 'lib/sevgi/geometry/equation/line/diagonal.rb', line 24 def hash [ self.class, slope, intercept ].hash end |
#intersection(other) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/sevgi/geometry/equation/line/diagonal.rb', line 28 def intersection(other) case other when Diagonal then y = y(x = (other.intercept - intercept) / (slope - other.slope)) when Horizontal then x = x(y = other.y) when Vertical then y = y(x = other.x) end Point[x, y] end |
#left?(point) ⇒ Boolean
38 39 40 |
# File 'lib/sevgi/geometry/equation/line/diagonal.rb', line 38 def left?(point) F.gt?(point.y, y(point.x)) end |
#onto?(point) ⇒ Boolean
42 43 44 |
# File 'lib/sevgi/geometry/equation/line/diagonal.rb', line 42 def onto?(point) F.eq?(point.y, y(point.x)) end |
#right?(point) ⇒ Boolean
46 47 48 |
# File 'lib/sevgi/geometry/equation/line/diagonal.rb', line 46 def right?(point) F.lt?(point.y, y(point.x)) end |
#shift(distance = nil, dx: nil, dy: nil) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/sevgi/geometry/equation/line/diagonal.rb', line 50 def shift(distance = nil, dx: nil, dy: nil) dx ||= 0.0 dy ||= 0.0 if distance dx += F.rx(distance, direction) dy -= F.ry(distance, direction) end Diagonal.new(slope:, intercept: intercept - slope * dx + dy) end |
#to_s ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/sevgi/geometry/equation/line/diagonal.rb', line 62 def to_s strings = [] strings << "#{F.approx(slope)} * x" if F.nonzero?(slope) strings << F.approx(intercept).abs.to_s if F.nonzero?(intercept) "L<y = #{strings.join(intercept.positive? ? " + " : " - ")}>" end |
#x(y) ⇒ Object
71 72 73 |
# File 'lib/sevgi/geometry/equation/line/diagonal.rb', line 71 def x(y) (y - intercept) / slope end |
#y(x) ⇒ Object
75 76 77 |
# File 'lib/sevgi/geometry/equation/line/diagonal.rb', line 75 def y(x) (slope * x) + intercept end |