Class: MapKit::BoundingBox

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

Overview

The class represents a bounding box specified by a top/left point and a bottom/right point (the coordinates can be pixels or degrees)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(top, left, bottom, right, zoom = nil) ⇒ BoundingBox

initialize the bounding box using the positions of two points and a optional zoom level

     top
left o------+
     |      |
     |      |
     +------o right
            bottom


72
73
74
# File 'lib/mapkit.rb', line 72

def initialize(top, left, bottom, right, zoom = nil)
  @top, @left, @bottom, @right, @zoom = top, left, bottom, right, zoom
end

Instance Attribute Details

#bottomObject

Returns the value of attribute bottom.



60
61
62
# File 'lib/mapkit.rb', line 60

def bottom
  @bottom
end

#leftObject

Returns the value of attribute left.



60
61
62
# File 'lib/mapkit.rb', line 60

def left
  @left
end

#rightObject

Returns the value of attribute right.



60
61
62
# File 'lib/mapkit.rb', line 60

def right
  @right
end

#topObject

Returns the value of attribute top.



60
61
62
# File 'lib/mapkit.rb', line 60

def top
  @top
end

#zoomObject

Returns the value of attribute zoom.



60
61
62
# File 'lib/mapkit.rb', line 60

def zoom
  @zoom
end

Instance Method Details

#centerObject

returns [lat, lnt] of bounding box



97
98
99
# File 'lib/mapkit.rb', line 97

def center
  [@left + width / 2, @bottom + height / 2]
end

#coordsObject

returns array of [top, left, bottom, right]



77
78
79
# File 'lib/mapkit.rb', line 77

def coords
  [@top, @left, @bottom, @right]
end

#grow(percent) ⇒ Object

grow bounding box by percentage and return new bounding box object



112
113
114
115
116
# File 'lib/mapkit.rb', line 112

def grow(percent)
  copy = self.clone
  copy.grow!(percent)
  copy
end

#grow!(percent) ⇒ Object

grow bounding box by percentage



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

def grow!(percent)
  lng = ((100.0 + percent) * (width / 2.0 / 100.0)) / 2.0
  lat = ((100.0 + percent) * (height / 2.0 / 100.0)) / 2.0
  @top += lat
  @left -= lng
  @bottom -= lat
  @right += lng
end

#heightObject

returns the height of the bounding box in degrees



87
88
89
# File 'lib/mapkit.rb', line 87

def height
  @top - @bottom
end

#sspnObject

returns array of [width, height] of sspn



92
93
94
# File 'lib/mapkit.rb', line 92

def sspn
  [width / 2, height / 2]
end

#widthObject

returns the width of the bounding box in degrees



82
83
84
# File 'lib/mapkit.rb', line 82

def width
  @right - @left
end