Class: Geospatial::Circle

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(center, radius) ⇒ Circle

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



31
32
33
34
# File 'lib/geospatial/circle.rb', line 31

def initialize(center, radius)
	@center = center
	@radius = radius
end

Instance Attribute Details

#centerObject (readonly)

Returns the value of attribute center.



36
37
38
# File 'lib/geospatial/circle.rb', line 36

def center
  @center
end

#radiusObject (readonly)

Returns the value of attribute radius.



37
38
39
# File 'lib/geospatial/circle.rb', line 37

def radius
  @radius
end

Instance Method Details

#distance_from(point) ⇒ Object



43
44
45
# File 'lib/geospatial/circle.rb', line 43

def distance_from(point)
	Location.new(point[0], point[1]).distance_from(@center)
end

#include?(other) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
# 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) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
# 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) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
# 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) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/geospatial/circle.rb', line 47

def include_point?(point, radius = @radius)
	distance_from(point) <= radius
end

#intersect?(other) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
# 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) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
101
102
103
# 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) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/geospatial/circle.rb', line 105

def intersect_with_circle?(other)
	include_point?(other.center.to_a, @radius + other.radius)
end

#midpoints {|[@bounds[:longitude].begin, @center.latitude]| ... } ⇒ Object

Yields:

  • ([@bounds[:longitude].begin, @center.latitude])


83
84
85
86
87
88
89
90
# 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_sObject



39
40
41
# File 'lib/geospatial/circle.rb', line 39

def to_s
	"#{self.class}[#{@center}, #{@radius}]"
end