Class: Wallbum::ImagePacking::Rectangle

Inherits:
Object
  • Object
show all
Defined in:
lib/wallbum/rectangle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#childrenObject (readonly)

Returns the value of attribute children.



11
12
13
# File 'lib/wallbum/rectangle.rb', line 11

def children
  @children
end

#heightObject (readonly)

Returns the value of attribute height.



10
11
12
# File 'lib/wallbum/rectangle.rb', line 10

def height
  @height
end

#imageObject

Returns the value of attribute image.



12
13
14
# File 'lib/wallbum/rectangle.rb', line 12

def image
  @image
end

#widthObject (readonly)

Returns the value of attribute width.



9
10
11
# File 'lib/wallbum/rectangle.rb', line 9

def width
  @width
end

#xObject (readonly)

Returns the value of attribute x.



7
8
9
# File 'lib/wallbum/rectangle.rb', line 7

def x
  @x
end

#yObject (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_childrenObject



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

#areaObject



36
37
38
# File 'lib/wallbum/rectangle.rb', line 36

def area
  @width * @height
end

#child_countObject



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

Returns:

  • (Boolean)


32
33
34
# File 'lib/wallbum/rectangle.rb', line 32

def empty?
  @image.nil?
end

#empty_childObject



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_childObject



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

Returns:

  • (Boolean)


24
25
26
# File 'lib/wallbum/rectangle.rb', line 24

def leaf?
  @children.empty?
end

#root?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/wallbum/rectangle.rb', line 28

def root?
  @parent.nil?
end

#splitObject



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