Class: Castaway::Box

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a, b, c, d, other = {}) ⇒ Box

Returns a new instance of Box.



13
14
15
16
17
18
19
# 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

Class Method Details

.from_size(size) ⇒ Object



5
6
7
8
9
10
11
# 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

Instance Method Details

#[](id) ⇒ Object



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

def [](id)
  @other_points[id]
end

#[]=(id, point_of_interest) ⇒ Object



21
22
23
24
# File 'lib/castaway/box.rb', line 21

def []=(id, point_of_interest)
  @other_points[id] = point_of_interest
  self
end

#boundsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# 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_sObject



73
74
75
# File 'lib/castaway/box.rb', line 73

def to_s
  "#{@a}-#{@b}-#{@c}-#{@d}"
end