Class: CTioga2::Graphics::Line
- Inherits:
-
Object
- Object
- CTioga2::Graphics::Line
- Defined in:
- lib/ctioga2/graphics/geometry.rb
Overview
A geometry line, ie something that has a starting point and a direction. It is infinite
Direct Known Subclasses
Instance Attribute Summary collapse
-
#dx ⇒ Object
Returns the value of attribute dx.
-
#dy ⇒ Object
Returns the value of attribute dy.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
-
#initialize(x, y, dx, dy) ⇒ Line
constructor
A new instance of Line.
-
#intersection(line) ⇒ Object
Returns the X and Y positions of the intersection with the given Line, or false should there be none.
-
#within_bounds?(x, y) ⇒ Boolean
Checks if within the bounds of the line (but not necessarily ON the line).
Constructor Details
#initialize(x, y, dx, dy) ⇒ Line
Returns a new instance of Line.
35 36 37 38 39 40 |
# File 'lib/ctioga2/graphics/geometry.rb', line 35 def initialize(x, y, dx, dy) @x = x.to_f @y = y.to_f @dx = dx.to_f @dy = dy.to_f end |
Instance Attribute Details
#dx ⇒ Object
Returns the value of attribute dx.
33 34 35 |
# File 'lib/ctioga2/graphics/geometry.rb', line 33 def dx @dx end |
#dy ⇒ Object
Returns the value of attribute dy.
33 34 35 |
# File 'lib/ctioga2/graphics/geometry.rb', line 33 def dy @dy end |
#x ⇒ Object
Returns the value of attribute x.
33 34 35 |
# File 'lib/ctioga2/graphics/geometry.rb', line 33 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
33 34 35 |
# File 'lib/ctioga2/graphics/geometry.rb', line 33 def y @y end |
Instance Method Details
#intersection(line) ⇒ Object
Returns the X and Y positions of the intersection with the given Line, or false should there be none.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ctioga2/graphics/geometry.rb', line 44 def intersection(line) fact = @dx * line.dy - line.dx * @dy rhs = @dy * (line.x - @x) - @dx * (line.y - @y) if fact != 0 # There is a unique intersection beta = rhs/fact nx = line.x + beta * line.dx ny = line.y + beta * line.dy # elsif rhs == 0 # # Infinite, we return # return else return false end return [nx, ny] if (within_bounds?(nx, ny) and line.within_bounds?(nx, ny)) return false end |
#within_bounds?(x, y) ⇒ Boolean
Checks if within the bounds of the line (but not necessarily ON the line)
29 30 31 |
# File 'lib/ctioga2/graphics/geometry.rb', line 29 def within_bounds?(x, y) return true end |