Class: CTioga2::Graphics::Line

Inherits:
Object
  • Object
show all
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

Segment

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dxObject

Returns the value of attribute dx.



33
34
35
# File 'lib/ctioga2/graphics/geometry.rb', line 33

def dx
  @dx
end

#dyObject

Returns the value of attribute dy.



33
34
35
# File 'lib/ctioga2/graphics/geometry.rb', line 33

def dy
  @dy
end

#xObject

Returns the value of attribute x.



33
34
35
# File 'lib/ctioga2/graphics/geometry.rb', line 33

def x
  @x
end

#yObject

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)

Returns:

  • (Boolean)


29
30
31
# File 'lib/ctioga2/graphics/geometry.rb', line 29

def within_bounds?(x, y)
  return true
end