Class: Geospatial::Box

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin, size, max = nil) ⇒ Box

Returns a new instance of Box.


52
53
54
55
56
# File 'lib/geospatial/box.rb', line 52

def initialize(origin, size, max = nil)
	@origin = origin
	@size = size
	@max = max
end

Instance Attribute Details

#originObject (readonly)

Returns the value of attribute origin


64
65
66
# File 'lib/geospatial/box.rb', line 64

def origin
  @origin
end

#sizeObject (readonly)

Returns the value of attribute size


65
66
67
# File 'lib/geospatial/box.rb', line 65

def size
  @size
end

Class Method Details

.enclosing_points(points) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Object Also known as: []


26
27
28
# File 'lib/geospatial/box.rb', line 26

def from_bounds(min, max)
	self.new(min, max-min, max)
end

Instance Method Details

#centerObject


91
92
93
# File 'lib/geospatial/box.rb', line 91

def center
	@origin + (@size/2)
end

#corners {|@origin| ... } ⇒ Object

This yields the four corners of the box.

Yields:


80
81
82
83
84
85
86
87
88
89
# 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

#freezeObject


58
59
60
61
62
# File 'lib/geospatial/box.rb', line 58

def freeze
	self.max
	
	super
end

#include?(other) ⇒ Boolean

Returns:

  • (Boolean)

115
116
117
# File 'lib/geospatial/box.rb', line 115

def include?(other)
	include_point?(other.min) && include_point?(other.max)
end

#include_point?(point) ⇒ Boolean

Returns:

  • (Boolean)

107
108
109
110
111
112
113
# 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) ⇒ Boolean

Returns:

  • (Boolean)

119
120
121
122
123
124
125
126
127
128
# 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

#maxObject


75
76
77
# File 'lib/geospatial/box.rb', line 75

def max
	@max ||= @origin + @size
end

#midpoints {|Vector[@origin[0] + size[0] / 2, @origin[1]| ... } ⇒ Object

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

Yields:


96
97
98
99
100
101
102
103
104
105
# 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

#minObject


71
72
73
# File 'lib/geospatial/box.rb', line 71

def min
	@origin
end

#to_sObject


67
68
69
# File 'lib/geospatial/box.rb', line 67

def to_s
	"#{self.class}[#{min.inspect}, #{max.inspect}]"
end