class Geospatial::Circle

A circle is a geometric primative where the center is a location and the radius is in meters.

Attributes

center[R]
radius[R]

Public Class Methods

new(center, radius) click to toggle source

Center must be a vector, radius must be a numeric value.

# File lib/geospatial/circle.rb, line 31
def initialize(center, radius)
        @center = center
        @radius = radius
end

Public Instance Methods

distance_from(point) click to toggle source
# File lib/geospatial/circle.rb, line 43
def distance_from(point)
        Location.new(point[0], point[1]).distance_from(@center)
end
include?(other) click to toggle source
# File lib/geospatial/circle.rb, line 65
def include?(other)
        case other
        when Box
                include_box?(other)
        when Circle
                include_circle?(other)
        end
end
include_box?(other) click to toggle source
# File lib/geospatial/circle.rb, line 51
def include_box?(other)
        # We must contain the for corners of the other box:
        other.corners do |corner|
                return false unless include_point?(corner)
        end
        
        return true
end
include_circle?(other) click to toggle source
# File lib/geospatial/circle.rb, line 60
def include_circle?(other)
        # We must be big enough to contain the other point:
        @radius >= other.radius && include_point?(other.center.to_a, @radius - other.radius)
end
include_point?(point, radius = @radius) click to toggle source
# File lib/geospatial/circle.rb, line 47
def include_point?(point, radius = @radius)
        distance_from(point) <= radius
end
intersect?(other) click to toggle source
# File lib/geospatial/circle.rb, line 74
def intersect?(other)
        case other
        when Box
                intersect_with_box?(other)
        when Circle
                intersect_with_circle?(other)
        end
end
intersect_with_box?(other) click to toggle source
# File lib/geospatial/circle.rb, line 92
def intersect_with_box?(other)
        # If we contain any of the four corners:
        other.corners do |corner|
                return true if include_point?(corner)
        end
        
        midpoints do |midpoint|
                return true if other.include_point?(midpoint)
        end
        
        return false
end
intersect_with_circle?(other) click to toggle source
# File lib/geospatial/circle.rb, line 105
def intersect_with_circle?(other)
        include_point?(other.center.to_a, @radius + other.radius)
end
midpoints() { |begin, latitude| ... } click to toggle source
# File lib/geospatial/circle.rb, line 83
def midpoints
        @bounds ||= @center.bounding_box(@radius)
        
        yield([@bounds[:longitude].begin, @center.latitude])
        yield([@bounds[:longitude].end, @center.latitude])
        yield([@center.longitude, @bounds[:latitude].begin])
        yield([@center.longitude, @bounds[:latitude].end])
end
to_s() click to toggle source
# File lib/geospatial/circle.rb, line 39
def to_s
        "#{self.class}[#{@center}, #{@radius}]"
end