Class: GeoTreeModule::Bounds
- Inherits:
-
Object
- Object
- GeoTreeModule::Bounds
- Defined in:
- lib/geotree/bounds.rb
Instance Attribute Summary collapse
-
#h ⇒ Object
Returns the value of attribute h.
-
#w ⇒ Object
Returns the value of attribute w.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Class Method Summary collapse
- .intersect(a, b) ⇒ Object
-
.rnd ⇒ Object
Construct a random bounds.
- .rnd_many(count) ⇒ Object
Instance Method Summary collapse
- #contains_point(loc) ⇒ Object
- #flip ⇒ Object
-
#initialize(x = 0, y = 0, w = 0, h = 0) ⇒ Bounds
constructor
Constructor.
- #inspect ⇒ Object
- #to_s ⇒ Object
- #x2 ⇒ Object
- #y2 ⇒ Object
Constructor Details
#initialize(x = 0, y = 0, w = 0, h = 0) ⇒ Bounds
Constructor. If x is a Float, it assumes that x,y,w,h are expressed in terms of latitudes and longitudes, and converts them to integers accordingly.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/geotree/bounds.rb', line 10 def initialize(x=0,y=0,w=0,h=0) if x.is_a? Float x1 = Loc.cvt_latlong_to_int(x) y1 = Loc.cvt_latlong_to_int(y) w = Loc.cvt_latlong_to_int(x+w) - x1 h = Loc.cvt_latlong_to_int(y+h) - y1 x,y = x1,y1 end @x = x @y = y @w = w @h = h end |
Instance Attribute Details
#h ⇒ Object
Returns the value of attribute h.
5 6 7 |
# File 'lib/geotree/bounds.rb', line 5 def h @h end |
#w ⇒ Object
Returns the value of attribute w.
5 6 7 |
# File 'lib/geotree/bounds.rb', line 5 def w @w end |
#x ⇒ Object
Returns the value of attribute x.
5 6 7 |
# File 'lib/geotree/bounds.rb', line 5 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
5 6 7 |
# File 'lib/geotree/bounds.rb', line 5 def y @y end |
Class Method Details
.intersect(a, b) ⇒ Object
49 50 51 |
# File 'lib/geotree/bounds.rb', line 49 def self.intersect(a,b) a.x2 > b.x && b.x2 > a.x && a.y2 > b.y && b.y2 > a.y end |
.rnd ⇒ Object
Construct a random bounds
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/geotree/bounds.rb', line 55 def self.rnd x1 = rand(1000) y1 = rand(1000) x2 = rand(1000) y2 = rand(1000) x1,x2 = [x1,x2].min,[x1,x2].max y1,y2 = [y1,y2].min,[y1,y2].max sz = rand() * rand() * 1000 sz = [sz.to_i,1].max ix = [0,x2-x1-sz].max iy = [0,y2-y1-sz].max sx = (x2-x1-ix)/2 sy = (y2-y1-iy)/2 cx = (x1+x2)/2 cy = (y1+y2)/2 Bounds.new(cx-sx,cy-sy,sx*2,sy*2) end |
.rnd_many(count) ⇒ Object
74 75 76 77 78 |
# File 'lib/geotree/bounds.rb', line 74 def self.rnd_many(count) a = [] count.times{a << self.rnd} a end |
Instance Method Details
#contains_point(loc) ⇒ Object
41 42 43 |
# File 'lib/geotree/bounds.rb', line 41 def contains_point(loc) loc.x >= @x && loc.x < x2 && loc.y >= @y && loc.y < y2 end |
#flip ⇒ Object
45 46 47 |
# File 'lib/geotree/bounds.rb', line 45 def flip Bounds.new(@y,@x,@h,@w) end |
#inspect ⇒ Object
37 38 39 |
# File 'lib/geotree/bounds.rb', line 37 def inspect to_s end |
#to_s ⇒ Object
33 34 35 |
# File 'lib/geotree/bounds.rb', line 33 def to_s "[#{@x},#{@y}..#{x2},#{y2}]" end |
#x2 ⇒ Object
25 26 27 |
# File 'lib/geotree/bounds.rb', line 25 def x2 @x + @w end |
#y2 ⇒ Object
29 30 31 |
# File 'lib/geotree/bounds.rb', line 29 def y2 @y + @h end |