class Geospatial::Box

Attributes

origin[R]
size[R]

Public Class Methods

[](min, max)
Alias for: from_bounds
enclosing_points(points) click to toggle source
# File lib/geospatial/box.rb, line 32
def enclosing_points(points)
        return nil unless points.any?
        
        min = points.first.to_a
        max = points.first.to_a
        
        points.each do |point|
                point.each_with_index do |value, index|
                        if value < min[index]
                                min[index] = value
                        elsif value > max[index]
                                max[index] = value
                        end
                end
        end
        
        return self.from_bounds(Vector.elements(min), Vector.elements(max))
end
from_bounds(min, max) click to toggle source
# File lib/geospatial/box.rb, line 26
def from_bounds(min, max)
        self.new(min, max-min, max)
end
Also aliased as: []
new(origin, size, max = nil) click to toggle source
# File lib/geospatial/box.rb, line 52
def initialize(origin, size, max = nil)
        @origin = origin
        @size = size
        @max = max
end

Public Instance Methods

center() click to toggle source
# File lib/geospatial/box.rb, line 91
def center
        @origin + (@size/2)
end
corners() { |origin| ... } click to toggle source

This yields the four corners of the box.

# File lib/geospatial/box.rb, line 80
def corners
        return to_enum(:corners) unless block_given?
        
        yield(@origin)
        
        max = self.max
        yield(Vector[max[0], @origin[1]])
        yield(max)
        yield(Vector[@origin[0], max[1]])
end
freeze() click to toggle source
Calls superclass method
# File lib/geospatial/box.rb, line 58
def freeze
        self.max
        
        super
end
include?(other) click to toggle source
# File lib/geospatial/box.rb, line 115
def include?(other)
        include_point?(other.min) && include_point?(other.max)
end
include_point?(point) click to toggle source
# File lib/geospatial/box.rb, line 107
def include_point?(point)
        2.times do |i|
                return false if point[i] < min[i] or point[i] >= max[i]
        end
        
        return true
end
intersect?(other) click to toggle source
# File lib/geospatial/box.rb, line 119
def intersect?(other)
        2.times do |i|
                # Separating axis theorm, if the minimum of the other is past the maximum of self, or the maximum of other is less than the minimum of self, an intersection cannot occur.
                if other.min[i] > self.max[i] or other.max[i] < self.min[i]
                        return false
                end
        end
        
        return true
end
max() click to toggle source
# File lib/geospatial/box.rb, line 75
def max
        @max ||= @origin + @size
end
midpoints() { |Vector[origin + size / 2, origin| ... } click to toggle source

This yields the midpoints of the four sides of the box.

# File lib/geospatial/box.rb, line 96
def midpoints
        return to_enum(:midpoints) unless block_given?
        
        size = self.size
        
        yield(Vector[@origin[0] + size[0] / 2, @origin[1]])
        yield(Vector[@origin[0] + size[0], @origin[1] + size[1] / 2])
        yield(Vector[@origin[0] + size[0] / 2, @origin[1] + size[1]])
        yield(Vector[@origin[0], @origin[1] + size[1] / 2])
end
min() click to toggle source
# File lib/geospatial/box.rb, line 71
def min
        @origin
end
to_s() click to toggle source
# File lib/geospatial/box.rb, line 67
def to_s
        "#{self.class}[#{min.inspect}, #{max.inspect}]"
end