Class: GpsUtils::BoundingBox

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nw_point, se_point) ⇒ BoundingBox

Initialize BoundingBox.

Parameters:

  • nw_point (Point)

    North-West corner

  • se_point (Point)

    South-East corner



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/gpsutils.rb', line 81

def initialize(nw_point, se_point)
  unless nw_point.is_a? Point
    raise ArgumentError.new 'nw_point must be a Point.'
  end

  unless se_point.is_a? Point
    raise ArgumentError.new 'se_point must be a Point.'
  end

  @nw = nw_point
  @se = se_point

  @p21 = @se.lat - @nw.lat
  @p41 = @nw.lng - @se.lng

  @p21ms = @p21 ** 2
  @p41ms = @p41 ** 2
end

Instance Attribute Details

#nwPoint (readonly)

Returns South-East corner.

Returns:

  • (Point)

    South-East corner



71
72
73
# File 'lib/gpsutils.rb', line 71

def nw
  @nw
end

#sePoint (readonly)

Returns South-East corner.

Returns:

  • (Point)

    South-East corner



75
76
77
# File 'lib/gpsutils.rb', line 75

def se
  @se
end

Instance Method Details

#cover?(point) ⇒ Boolean

Determine whether point is inside bounding box.

Parameters:

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
# File 'lib/gpsutils.rb', line 103

def cover?(point)
  p = [point.lat - @nw.lat, point.lng - @se.lng]

  p21x = p[0] * @p21
  p41x = p[1] * @p41

  0 < p21x and p21x < @p21ms and 0 <= p41x and p41x <= @p41ms
end