Class: Mittsu::Box2
- Inherits:
-
Object
- Object
- Mittsu::Box2
- Defined in:
- lib/mittsu/math/box2.rb
Instance Attribute Summary collapse
-
#max ⇒ Object
Returns the value of attribute max.
-
#min ⇒ Object
Returns the value of attribute min.
Instance Method Summary collapse
- #==(box) ⇒ Object
- #center(target = Mittsu::Vector2.new) ⇒ Object
- #clamp_point(point, target = nil) ⇒ Object
- #clone ⇒ Object
- #contains_box?(box) ⇒ Boolean
- #contains_point?(point) ⇒ Boolean
- #copy(box) ⇒ Object
- #distance_to_point(point) ⇒ Object
- #empty? ⇒ Boolean
- #expand_by_point(point) ⇒ Object
- #expand_by_scalar(scalar) ⇒ Object
- #expand_by_vector(vector) ⇒ Object
-
#initialize(min = nil, max = nil) ⇒ Box2
constructor
A new instance of Box2.
- #intersect(box) ⇒ Object
- #intersection_box?(box) ⇒ Boolean
- #make_empty ⇒ Object
- #parameter(point, target = Mittsu::Vector2.new) ⇒ Object
- #set(min, max) ⇒ Object
- #set_from_center_and_size(center, size) ⇒ Object
- #set_from_points(points) ⇒ Object
- #size(target = Mittsu::Vector2.new) ⇒ Object
- #translate(offset) ⇒ Object
- #union(box) ⇒ Object
Constructor Details
#initialize(min = nil, max = nil) ⇒ Box2
Returns a new instance of Box2.
7 8 9 10 |
# File 'lib/mittsu/math/box2.rb', line 7 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
#max ⇒ Object
Returns the value of attribute max.
5 6 7 |
# File 'lib/mittsu/math/box2.rb', line 5 def max @max end |
#min ⇒ Object
Returns the value of attribute min.
5 6 7 |
# File 'lib/mittsu/math/box2.rb', line 5 def min @min end |
Instance Method Details
#==(box) ⇒ Object
126 127 128 |
# File 'lib/mittsu/math/box2.rb', line 126 def ==(box) box.min == @min && box.max == @max end |
#center(target = Mittsu::Vector2.new) ⇒ Object
50 51 52 |
# File 'lib/mittsu/math/box2.rb', line 50 def center(target = Mittsu::Vector2.new) target.add_vectors(@min, @max).multiply_scalar(0.5) end |
#clamp_point(point, target = nil) ⇒ Object
98 99 100 101 |
# File 'lib/mittsu/math/box2.rb', line 98 def clamp_point(point, target = nil) result = target || Mittsu::Vector2.new result.copy(point).clamp(@min, @max) end |
#clone ⇒ Object
130 131 132 |
# File 'lib/mittsu/math/box2.rb', line 130 def clone Mittsu::Box2.new.copy(self) end |
#contains_box?(box) ⇒ Boolean
80 81 82 |
# File 'lib/mittsu/math/box2.rb', line 80 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
76 77 78 |
# File 'lib/mittsu/math/box2.rb', line 76 def contains_point?(point) !(point.x < @min.x || point.x > @max.x || point.y < @min.y || point.y > @max.y) end |
#copy(box) ⇒ Object
33 34 35 36 37 |
# File 'lib/mittsu/math/box2.rb', line 33 def copy(box) @min.copy(box.min) @max.copy(box.max) self end |
#distance_to_point(point) ⇒ Object
103 104 105 106 |
# File 'lib/mittsu/math/box2.rb', line 103 def distance_to_point(point) clampedPoint = point.clone.clamp(@min, @max) clampedPoint.sub(point).length end |
#empty? ⇒ Boolean
45 46 47 48 |
# File 'lib/mittsu/math/box2.rb', line 45 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
58 59 60 61 62 |
# File 'lib/mittsu/math/box2.rb', line 58 def (point) @min.min(point) @max.max(point) self end |
#expand_by_scalar(scalar) ⇒ Object
70 71 72 73 74 |
# File 'lib/mittsu/math/box2.rb', line 70 def (scalar) @min.add_scalar(-scalar) @max.add_scalar(scalar) self end |
#expand_by_vector(vector) ⇒ Object
64 65 66 67 68 |
# File 'lib/mittsu/math/box2.rb', line 64 def (vector) @min.sub(vector) @max.add(vector) self end |
#intersect(box) ⇒ Object
108 109 110 111 112 |
# File 'lib/mittsu/math/box2.rb', line 108 def intersect(box) @min.max(box.min) @max.min(box.max) self end |
#intersection_box?(box) ⇒ Boolean
93 94 95 96 |
# File 'lib/mittsu/math/box2.rb', line 93 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_empty ⇒ Object
39 40 41 42 43 |
# File 'lib/mittsu/math/box2.rb', line 39 def make_empty @min.x = @min.y = Float::INFINITY @max.x = @max.y = - Float::INFINITY self end |
#parameter(point, target = Mittsu::Vector2.new) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/mittsu/math/box2.rb', line 84 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
12 13 14 15 16 |
# File 'lib/mittsu/math/box2.rb', line 12 def set(min, max) @min.copy(min) @max.copy(max) self end |
#set_from_center_and_size(center, size) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/mittsu/math/box2.rb', line 26 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
18 19 20 21 22 23 24 |
# File 'lib/mittsu/math/box2.rb', line 18 def set_from_points(points) self.make_empty points.each do |point| self.(point) end self end |
#size(target = Mittsu::Vector2.new) ⇒ Object
54 55 56 |
# File 'lib/mittsu/math/box2.rb', line 54 def size(target = Mittsu::Vector2.new) target.sub_vectors(@max, @min) end |
#translate(offset) ⇒ Object
120 121 122 123 124 |
# File 'lib/mittsu/math/box2.rb', line 120 def translate(offset) @min.add(offset) @max.add(offset) self end |
#union(box) ⇒ Object
114 115 116 117 118 |
# File 'lib/mittsu/math/box2.rb', line 114 def union(box) @min.min(box.min) @max.max(box.max) self end |