class Castaway::Box

Public Class Methods

from_size(size) click to toggle source
# File lib/castaway/box.rb, line 5
def self.from_size(size)
  new(
    Point.new(0, 0),
    Point.new(size.width - 1, 0),
    Point.new(size.width - 1, size.height - 1),
    Point.new(0, size.height - 1))
end
new(a, b, c, d, other = {}) click to toggle source
# File lib/castaway/box.rb, line 13
def initialize(a, b, c, d, other = {})
  @a = a
  @b = b
  @c = c
  @d = d
  @other_points = other.dup
end

Public Instance Methods

[](id) click to toggle source
# File lib/castaway/box.rb, line 26
def [](id)
  @other_points[id]
end
[]=(id, point_of_interest) click to toggle source
# File lib/castaway/box.rb, line 21
def []=(id, point_of_interest)
  @other_points[id] = point_of_interest
  self
end
bounds() click to toggle source
# File lib/castaway/box.rb, line 58
def bounds
  x1, x2 = [ @a.x, @b.x, @c.x, @d.x ].minmax
  y1, y2 = [ @a.y, @b.y, @c.y, @d.y ].minmax

  # translate to origin
  x2 -= x1
  y2 -= y1
  others = @other_points.each_with_object({}) do |(k, v), h|
    h[k] = v.translate(-x1, -y1)
  end

  Box.new(Point.new(0, 0), Point.new(x2, 0),
          Point.new(x2, y2), Point.new(0, y2), others)
end
rotate(degrees) click to toggle source
# File lib/castaway/box.rb, line 43
def rotate(degrees)
  rads = degrees * Math::PI / 180.0

  a = @a.rotate(rads)
  b = @b.rotate(rads)
  c = @c.rotate(rads)
  d = @d.rotate(rads)

  others = @other_points.each_with_object({}) do |(k, v), h|
    h[k] = v.rotate(rads)
  end

  Box.new(a, b, c, d, others)
end
scale(factor) click to toggle source
# File lib/castaway/box.rb, line 30
def scale(factor)
  a = @a * factor
  b = @b * factor
  c = @c * factor
  d = @d * factor

  others = @other_points.each_with_object({}) do |(k, v), h|
    h[k] = v * factor
  end

  Box.new(a, b, c, d, others)
end
to_s() click to toggle source
# File lib/castaway/box.rb, line 73
def to_s
  "#{@a}-#{@b}-#{@c}-#{@d}"
end