class CTioga2::Graphics::Line
A geometry line, ie something that has a starting point and a direction. It is infinite
Attributes
dx[RW]
dy[RW]
x[RW]
y[RW]
Public Class Methods
new(x, y, dx, dy)
click to toggle source
# 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
Public Instance Methods
intersection(line)
click to toggle source
Returns the X and Y positions of the intersection with the given Line
, or false should there be none.
# 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)
click to toggle source
Checks if within the bounds of the line (but not necessarily ON the line)
# File lib/ctioga2/graphics/geometry.rb, line 29 def within_bounds?(x, y) return true end