Class: Castaway::Box
- Inherits:
-
Object
- Object
- Castaway::Box
- Defined in:
- lib/castaway/box.rb
Class Method Summary collapse
Instance Method Summary collapse
- #[](id) ⇒ Object
- #[]=(id, point_of_interest) ⇒ Object
- #bounds ⇒ Object
-
#initialize(a, b, c, d, other = {}) ⇒ Box
constructor
A new instance of Box.
- #rotate(degrees) ⇒ Object
- #scale(factor) ⇒ Object
- #to_s ⇒ Object
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
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 |
#bounds ⇒ Object
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_s ⇒ Object
73 74 75 |
# File 'lib/castaway/box.rb', line 73 def to_s "#{@a}-#{@b}-#{@c}-#{@d}" end |