Class: Wallbum::ImagePacking::Rectangle
- Inherits:
-
Object
- Object
- Wallbum::ImagePacking::Rectangle
- Defined in:
- lib/wallbum/rectangle.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#image ⇒ Object
Returns the value of attribute image.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #all_children ⇒ Object
- #area ⇒ Object
- #child_count ⇒ Object
- #empty? ⇒ Boolean
- #empty_child ⇒ Object
-
#initialize(parent, x, y, width, height) ⇒ Rectangle
constructor
A new instance of Rectangle.
- #largest_child ⇒ Object
- #leaf? ⇒ Boolean
- #root? ⇒ Boolean
- #split ⇒ Object
Constructor Details
#initialize(parent, x, y, width, height) ⇒ Rectangle
Returns a new instance of Rectangle.
14 15 16 17 18 19 20 21 22 |
# File 'lib/wallbum/rectangle.rb', line 14 def initialize(parent, x, y, width, height) @parent = parent @width = width @height = height @x = x @y = y @children = [] @image = nil end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
11 12 13 |
# File 'lib/wallbum/rectangle.rb', line 11 def children @children end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
10 11 12 |
# File 'lib/wallbum/rectangle.rb', line 10 def height @height end |
#image ⇒ Object
Returns the value of attribute image.
12 13 14 |
# File 'lib/wallbum/rectangle.rb', line 12 def image @image end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
9 10 11 |
# File 'lib/wallbum/rectangle.rb', line 9 def width @width end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
7 8 9 |
# File 'lib/wallbum/rectangle.rb', line 7 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
8 9 10 |
# File 'lib/wallbum/rectangle.rb', line 8 def y @y end |
Instance Method Details
#all_children ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/wallbum/rectangle.rb', line 40 def all_children if leaf? self else @children.collect(&:all_children).flatten end end |
#area ⇒ Object
36 37 38 |
# File 'lib/wallbum/rectangle.rb', line 36 def area @width * @height end |
#child_count ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/wallbum/rectangle.rb', line 48 def child_count if leaf? 1 else @children.map(&:child_count).reduce(:+) end end |
#empty? ⇒ Boolean
32 33 34 |
# File 'lib/wallbum/rectangle.rb', line 32 def empty? @image.nil? end |
#empty_child ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/wallbum/rectangle.rb', line 64 def empty_child if leaf? if empty? self else nil end else @children.map(&:empty_child).compact.first end end |
#largest_child ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/wallbum/rectangle.rb', line 56 def largest_child if leaf? self else @children.map(&:largest_child).sort_by { |rect| rect.area }.last end end |
#leaf? ⇒ Boolean
24 25 26 |
# File 'lib/wallbum/rectangle.rb', line 24 def leaf? @children.empty? end |
#root? ⇒ Boolean
28 29 30 |
# File 'lib/wallbum/rectangle.rb', line 28 def root? @parent.nil? end |
#split ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/wallbum/rectangle.rb', line 76 def split if @width > @height split_horz else split_vert end end |