Class: Geospatial::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/geospatial/line.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a, b) ⇒ Line



25
26
27
28
# File 'lib/geospatial/line.rb', line 25

def initialize(a, b)
  @a = a
  @b = b
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



30
31
32
# File 'lib/geospatial/line.rb', line 30

def a
  @a
end

#bObject (readonly)

Returns the value of attribute b.



31
32
33
# File 'lib/geospatial/line.rb', line 31

def b
  @b
end

Instance Method Details

#intersect?(other) ⇒ Boolean



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/geospatial/line.rb', line 37

def intersect?(other)
  t = self.offset
  o = other.offset

  d = (o[1] * t[0]) - (o[0] * t[1])

  return false if d.zero?

  na = o[0] * (self.a[1] - other.a[1]) - o[1] * (self.a[0] - other.a[0])
  
  left_time = na.fdiv(d);

  if left_time < 0.0 or left_time > 1.0
    return false
  end

  nb = t[0] * (self.a[1] - other.a[1]) - t[1] * (self.a[0] - other.a[0])
  
  right_time = nb.fdiv(d)

  if right_time < 0.0 or right_time > 1.0
    return false
  end

  return left_time
end

#offsetObject



33
34
35
# File 'lib/geospatial/line.rb', line 33

def offset
  @b - @a
end