class Mittsu::Box2

Attributes

max[RW]
min[RW]

Public Class Methods

new(min = nil, max = nil) click to toggle source
# 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

Public Instance Methods

==(box) click to toggle source
# File lib/mittsu/math/box2.rb, line 124
def ==(box)
  box.min == @min && box.max == @max
end
center(target = Mittsu::Vector2.new) click to toggle source
# 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) click to toggle source
# 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
clone() click to toggle source
# File lib/mittsu/math/box2.rb, line 128
def clone
  Mittsu::Box2.new.copy(self)
end
contains_box?(box) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/mittsu/math/box2.rb, line 62
def expand_by_vector(vector)
  @min.sub(vector)
  @max.add(vector)
  self
end
intersect(box) click to toggle source
# File lib/mittsu/math/box2.rb, line 106
def intersect(box)
  @min.max(box.min)
  @max.min(box.max)
  self
end
intersection_box?(box) click to toggle source
# 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_empty() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/mittsu/math/box2.rb, line 52
def size(target = Mittsu::Vector2.new)
  target.sub_vectors(@max, @min)
end
translate(offset) click to toggle source
# File lib/mittsu/math/box2.rb, line 118
def translate(offset)
  @min.add(offset)
  @max.add(offset)
  self
end
union(box) click to toggle source
# File lib/mittsu/math/box2.rb, line 112
def union(box)
  @min.min(box.min)
  @max.max(box.max)
  self
end