Class: Sumi::Geometry::Equation::Linear::Diagonal
- Inherits:
-
Linear
- Object
- Linear
- Sumi::Geometry::Equation::Linear::Diagonal
- Defined in:
- geometry/lib/sumi/geometry/equation/linear.rb
Overview
Non-axis-aligned linear equation in y = slope * x + intercept form.
Instance Attribute Summary collapse
-
#intercept ⇒ Float
readonly
Returns the y-intercept.
-
#slope ⇒ Float
readonly
Returns the line slope.
Instance Method Summary collapse
-
#approx(precision = nil) ⇒ Sumi::Geometry::Equation::Linear::Diagonal
Returns an equation rounded to precision.
-
#eql?(other) ⇒ Boolean
(also: #==)
Reports strict equation equality.
-
#hash ⇒ Integer
Returns a hash compatible with strict equality.
-
#initialize(slope:, intercept:, origin: nil) ⇒ void
constructor
Creates a diagonal linear equation.
-
#on?(point) ⇒ Boolean
Reports whether a point is on the line.
-
#shift(distance = nil, dx: nil, dy: nil) ⇒ Sumi::Geometry::Equation::Linear::Diagonal
Returns a parallel equation shifted by a signed perpendicular offset.
-
#to_s ⇒ String
Formats the equation for display.
-
#x(y) ⇒ Float
Evaluates x for a y coordinate.
-
#y(x) ⇒ Float
Evaluates y for an x coordinate.
Constructor Details
#initialize(slope:, intercept:, origin: nil) ⇒ void
Creates a diagonal linear equation.
79 80 81 82 83 84 85 86 |
# File 'geometry/lib/sumi/geometry/equation/linear.rb', line 79 def initialize(slope:, intercept:, origin: nil) super() @slope = Real[:slope, slope] @intercept = Real[:intercept, intercept] @origin = Tuple[Point, origin] if origin Error.("A diagonal equation requires a non-zero slope") if @slope.zero? end |
Instance Attribute Details
#intercept ⇒ Float (readonly)
Returns the y-intercept.
71 72 73 |
# File 'geometry/lib/sumi/geometry/equation/linear.rb', line 71 def intercept @intercept end |
#slope ⇒ Float (readonly)
Returns the line slope.
67 68 69 |
# File 'geometry/lib/sumi/geometry/equation/linear.rb', line 67 def slope @slope end |
Instance Method Details
#approx(precision = nil) ⇒ Sumi::Geometry::Equation::Linear::Diagonal
Returns an equation rounded to precision.
91 92 93 |
# File 'geometry/lib/sumi/geometry/equation/linear.rb', line 91 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.
98 |
# File 'geometry/lib/sumi/geometry/equation/linear.rb', line 98 def eql?(other) = Nonvertical.equal?(self, other) |
#hash ⇒ Integer
Returns a hash compatible with strict equality.
102 |
# File 'geometry/lib/sumi/geometry/equation/linear.rb', line 102 def hash = Nonvertical.hash(self) |
#on?(point) ⇒ Boolean
Reports whether a point is on the line.
108 |
# File 'geometry/lib/sumi/geometry/equation/linear.rb', line 108 def on?(point) = Nonvertical.on?(self, point) |
#shift(distance = nil, dx: nil, dy: nil) ⇒ Sumi::Geometry::Equation::Linear::Diagonal
Returns a parallel equation shifted by a signed perpendicular offset. Positive distance moves to screen-left of the equation's canonical increasing-x direction.
117 118 119 120 121 122 123 124 |
# File 'geometry/lib/sumi/geometry/equation/linear.rb', line 117 def shift(distance = nil, dx: nil, dy: nil) distance, dx, dy = shift_values(distance, dx, dy) angle = F.atan(slope) dx += distance * F.sin(angle) dy -= distance * F.cos(angle) translated(dx, dy) end |
#to_s ⇒ String
Formats the equation for display.
128 129 130 131 132 133 134 135 |
# File 'geometry/lib/sumi/geometry/equation/linear.rb', line 128 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.
141 142 143 144 |
# File 'geometry/lib/sumi/geometry/equation/linear.rb', line 141 def x(y) y = Real[:y, y] @origin ? @origin.x + ((y - @origin.y) / slope) : (y - intercept) / slope end |
#y(x) ⇒ Float
Evaluates y for an x coordinate.
150 151 152 153 154 |
# File 'geometry/lib/sumi/geometry/equation/linear.rb', line 150 def y(x) return Nonvertical.y(self, x) unless @origin @origin.y + (slope * (Real[:x, x] - @origin.x)) end |