Class: Mittsu::Box2

Inherits:
Object
  • Object
show all
Defined in:
lib/mittsu/math/box2.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min = nil, max = nil) ⇒ Box2

Returns a new instance of Box2.



5
6
7
8
# File 'lib/mittsu/math/box2.rb', line 5

def initialize(min = nil, max = nil)
  @min = min || Mittsu::Vector2.new(Float::INFINITY, Float::INFINITY)
  @max = max || Mittsu::Vector2.new(-Float::INFINITY, -Float::INFINITY)
end

Instance Attribute Details

#maxObject

Returns the value of attribute max.



3
4
5
# File 'lib/mittsu/math/box2.rb', line 3

def max
  @max
end

#minObject

Returns the value of attribute min.



3
4
5
# File 'lib/mittsu/math/box2.rb', line 3

def min
  @min
end

Instance Method Details

#==(box) ⇒ Object



124
125
126
# File 'lib/mittsu/math/box2.rb', line 124

def ==(box)
  box.min == @min && box.max == @max
end

#center(target = Mittsu::Vector2.new) ⇒ Object



48
49
50
# File 'lib/mittsu/math/box2.rb', line 48

def center(target = Mittsu::Vector2.new)
  target.add_vectors(@min, @max).multiply_scalar(0.5)
end

#clamp_point(point, target = nil) ⇒ Object



96
97
98
99
# File 'lib/mittsu/math/box2.rb', line 96

def clamp_point(point, target = nil)
  result = target || Mittsu::Vector2.new
  result.copy(point).clamp(@min, @max)
end

#cloneObject



128
129
130
# File 'lib/mittsu/math/box2.rb', line 128

def clone
  Mittsu::Box2.new.copy(self)
end

#contains_box?(box) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/mittsu/math/box2.rb', line 78

def contains_box?(box)
  ((@min.x <= box.min.x) && (box.max.x <= @max.x) && (@min.y <= box.min.y) && (box.max.y <= @max.y))
end

#contains_point?(point) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/mittsu/math/box2.rb', line 74

def contains_point?(point)
  !(point.x < @min.x || point.x > @max.x || point.y < @min.y || point.y > @max.y)
end

#copy(box) ⇒ Object



31
32
33
34
35
# File 'lib/mittsu/math/box2.rb', line 31

def copy(box)
  @min.copy(box.min)
  @max.copy(box.max)
  self
end

#distance_to_point(point) ⇒ Object



101
102
103
104
# File 'lib/mittsu/math/box2.rb', line 101

def distance_to_point(point)
  clampedPoint = point.clone.clamp(@min, @max)
  clampedPoint.sub(point).length
end

#empty?Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/mittsu/math/box2.rb', line 43

def empty?
  # this is a more robust check for empty than (volume <= 0) because volume can get positive with two negative axes
  (@max.x < @min.x) || (@max.y < @min.y)
end

#expand_by_point(point) ⇒ Object



56
57
58
59
60
# File 'lib/mittsu/math/box2.rb', line 56

def expand_by_point(point)
  @min.min(point)
  @max.max(point)
  self
end

#expand_by_scalar(scalar) ⇒ Object



68
69
70
71
72
# File 'lib/mittsu/math/box2.rb', line 68

def expand_by_scalar(scalar)
  @min.add_scalar(-scalar)
  @max.add_scalar(scalar)
  self
end

#expand_by_vector(vector) ⇒ Object



62
63
64
65
66
# File 'lib/mittsu/math/box2.rb', line 62

def expand_by_vector(vector)
  @min.sub(vector)
  @max.add(vector)
  self
end

#intersect(box) ⇒ Object



106
107
108
109
110
# File 'lib/mittsu/math/box2.rb', line 106

def intersect(box)
  @min.max(box.min)
  @max.min(box.max)
  self
end

#intersection_box?(box) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
# File 'lib/mittsu/math/box2.rb', line 91

def intersection_box?(box)
  # using 6 splitting planes to rule out intersections.
  !(box.max.x < @min.x || box.min.x > @max.x || box.max.y < @min.y || box.min.y > @max.y)
end

#make_emptyObject



37
38
39
40
41
# File 'lib/mittsu/math/box2.rb', line 37

def make_empty
  @min.x = @min.y = Float::INFINITY
  @max.x = @max.y = - Float::INFINITY
  self
end

#parameter(point, target = Mittsu::Vector2.new) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/mittsu/math/box2.rb', line 82

def parameter(point, target = Mittsu::Vector2.new)
  # This can potentially have a divide by zero if the box
  # has a size dimension of 0.
  target.set(
    (point.x - @min.x) / (@max.x - @min.x),
    (point.y - @min.y) / (@max.y - @min.y)
  )
end

#set(min, max) ⇒ Object



10
11
12
13
14
# File 'lib/mittsu/math/box2.rb', line 10

def set(min, max)
  @min.copy(min)
  @max.copy(max)
  self
end

#set_from_center_and_size(center, size) ⇒ Object



24
25
26
27
28
29
# File 'lib/mittsu/math/box2.rb', line 24

def set_from_center_and_size(center, size)
  halfSize = size.clone.multiply_scalar(0.5)
  @min.copy(center).sub(halfSize)
  @max.copy(center).add(halfSize)
  self
end

#set_from_points(points) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/mittsu/math/box2.rb', line 16

def set_from_points(points)
  self.make_empty
  points.each do |point|
    self.expand_by_point(point)
  end
  self
end

#size(target = Mittsu::Vector2.new) ⇒ Object



52
53
54
# File 'lib/mittsu/math/box2.rb', line 52

def size(target = Mittsu::Vector2.new)
  target.sub_vectors(@max, @min)
end

#translate(offset) ⇒ Object



118
119
120
121
122
# File 'lib/mittsu/math/box2.rb', line 118

def translate(offset)
  @min.add(offset)
  @max.add(offset)
  self
end

#union(box) ⇒ Object



112
113
114
115
116
# File 'lib/mittsu/math/box2.rb', line 112

def union(box)
  @min.min(box.min)
  @max.max(box.max)
  self
end