class BinPacking::Heuristics::Base

Public Instance Methods

find_position_for_new_node!(box, free_rectangles) click to toggle source
# File lib/bin_packing/heuristics/base.rb, line 4
def find_position_for_new_node!(box, free_rectangles)
  best_score = BinPacking::Score.new
  width = box.width
  height = box.height

  free_rectangles.each do |free_rect|
    try_place_rect_in(free_rect, box, width, height, best_score)

    if box.can_rotate?
      try_place_rect_in(free_rect, box, height, width, best_score)
    end
  end

  best_score
end

Private Instance Methods

calculate_score(free_rect, rect_width, rect_height) click to toggle source
# File lib/bin_packing/heuristics/base.rb, line 36
def calculate_score(free_rect, rect_width, rect_height)
  raise NotImplementedError
end
try_place_rect_in(free_rect, box, rect_width, rect_height, best_score) click to toggle source
# File lib/bin_packing/heuristics/base.rb, line 22
def try_place_rect_in(free_rect, box, rect_width, rect_height, best_score)
  if free_rect.width >= rect_width && free_rect.height >= rect_height
    score = calculate_score(free_rect, rect_width, rect_height)
    if score > best_score
      box.x = free_rect.x
      box.y = free_rect.y
      box.width = rect_width
      box.height = rect_height
      box.packed = true
      best_score.assign(score)
    end
  end
end